postgresql/src/interfaces/ecpg/preproc
Tom Lane 6f0cef9353 Fix assorted bugs in ecpg's macro mechanism.
The code associated with EXEC SQL DEFINE was unreadable and full of
bugs, notably:

* It'd attempt to free a non-malloced string if the ecpg program
tries to redefine a macro that was defined on the command line.

* Possible memory stomp if user writes "-D=foo".

* Undef'ing or redefining a macro defined on the command line would
change the state visible to the next file, when multiple files are
specified on the command line.  (While possibly that could have been
an intentional choice, the code clearly intends to revert to the
original macro state; it's just failing to consider this interaction.)

* Missing "break" in defining a new macro meant that redefinition
of an existing name would cause an extra entry to be added to the
definition list.  While not immediately harmful, a subsequent undef
would result in the prior entry becoming visible again.

* The interactions with input buffering are subtle and were entirely
undocumented.

It's not that surprising that we hadn't noticed these bugs,
because there was no test coverage at all of either the -D
command line switch or multiple input files.  This patch adds
such coverage (in a rather hacky way I guess).

In addition to the code bugs, the user documentation was confused
about whether the -D switch defines a C macro or an ecpg one, and
it failed to mention that you can write "-Dsymbol=value".

These problems are old, so back-patch to all supported branches.

Discussion: https://postgr.es/m/998011.1713217712@sss.pgh.pa.us
2024-04-16 12:31:42 -04:00
..
po Update copyright for 2024 2024-01-03 20:49:05 -05:00
.gitignore Replace the data structure used for keyword lookup. 2019-01-06 17:02:57 -05:00
Makefile Update copyright for 2024 2024-01-03 20:49:05 -05:00
README.parser Move parse2.pl to parse.pl 2011-06-14 07:34:00 +03:00
c_keywords.c Harmonize parameter names in ecpg code. 2022-09-22 12:53:20 -07:00
c_kwlist.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
check_rules.pl Update copyright for 2024 2024-01-03 20:49:05 -05:00
descriptor.c Remove redundant null pointer checks before free() 2022-07-03 11:47:15 +02:00
ecpg.addons Make subquery aliases optional in the FROM clause. 2022-07-20 09:29:42 +01:00
ecpg.c Fix assorted bugs in ecpg's macro mechanism. 2024-04-16 12:31:42 -04:00
ecpg.header Harmonize parameter names in ecpg code. 2022-09-22 12:53:20 -07:00
ecpg.tokens Reduce size of backend scanner's tables. 2020-01-13 15:04:31 -05:00
ecpg.trailer Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
ecpg.type Fix ECPG's handling of type names that match SQL keywords. 2022-07-12 17:05:46 -04:00
ecpg_keywords.c Make the order of the header file includes consistent in non-backend modules. 2019-10-25 07:41:52 +05:30
ecpg_kwlist.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
keywords.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
nls.mk Revert "Use wildcards instead of manually-maintained file lists in */nls.mk." 2022-07-13 14:29:10 -04:00
output.c Harmonize parameter names in ecpg code. 2022-09-22 12:53:20 -07:00
parse.pl Fix ecpg's mechanism for detecting unsupported cases in the grammar. 2024-04-04 15:31:53 -04:00
parser.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pgc.l Fix assorted bugs in ecpg's macro mechanism. 2024-04-16 12:31:42 -04:00
preproc_extern.h Harmonize parameter names in ecpg code. 2022-09-22 12:53:20 -07:00
type.c Fix some typos 2024-01-03 14:22:54 +09:00
type.h Fix assorted bugs in ecpg's macro mechanism. 2024-04-16 12:31:42 -04:00
variable.c Remove duplicate lines of code 2023-04-24 11:16:17 +02:00

README.parser

ECPG modifies and extends the core grammar in a way that
1) every token in ECPG is <str> type. New tokens are
   defined in ecpg.tokens, types are defined in ecpg.type
2) most tokens from the core grammar are simply converted
   to literals concatenated together to form the SQL string
   passed to the server, this is done by parse.pl.
3) some rules need side-effects, actions are either added
   or completely overridden (compared to the basic token
   concatenation) for them, these are defined in ecpg.addons,
   the rules for ecpg.addons are explained below.
4) new grammar rules are needed for ECPG metacommands.
   These are in ecpg.trailer.
5) ecpg.header contains common functions, etc. used by
   actions for grammar rules.

In "ecpg.addons", every modified rule follows this pattern:
       ECPG: dumpedtokens postfix
where "dumpedtokens" is simply tokens from core gram.y's
rules concatenated together. e.g. if gram.y has this:
       ruleA: tokenA tokenB tokenC {...}
then "dumpedtokens" is "ruleAtokenAtokenBtokenC".
"postfix" above can be:
a) "block" - the automatic rule created by parse.pl is completely
    overridden, the code block has to be written completely as
    it were in a plain bison grammar
b) "rule" - the automatic rule is extended on, so new syntaxes
    are accepted for "ruleA". E.g.:
      ECPG: ruleAtokenAtokenBtokenC rule
          | tokenD tokenE { action_code; }
          ...
    It will be substituted with:
      ruleA: <original syntax forms and actions up to and including
                    "tokenA tokenB tokenC">
             | tokenD tokenE { action_code; }
             ...
c) "addon" - the automatic action for the rule (SQL syntax constructed
    from the tokens concatenated together) is prepended with a new
    action code part. This code part is written as is's already inside
    the { ... }

Multiple "addon" or "block" lines may appear together with the
new code block if the code block is common for those rules.