1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-06 07:49:08 +03:00
Files
postgres/src/interfaces/ecpg/test/sql/define.pgc
Tom Lane 392e6e9e6a 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:32 -04:00

84 lines
2.1 KiB
Plaintext

exec sql include sqlca;
exec sql include ../regression;
exec sql define STR 'abcdef';
exec sql define INSERTNULL 1;
exec sql define NUMBER 29;
int main(void)
{
exec sql begin declare section;
int i;
char s[200];
exec sql end declare section;
ECPGdebug(1, stderr);
exec sql whenever sqlerror do sqlprint();
exec sql connect to REGRESSDB1;
exec sql create table test (a int, b text);
exec sql insert into test values (NUMBER, STR);
exec sql ifdef INSERTNULL;
exec sql insert into test values (NULL, 'defined');
exec sql endif;
exec sql ifndef INSERTNULL;
exec sql insert into test values (NULL, 'not defined');
exec sql elif SOMEOTHERVAR;
exec sql insert into test values (NULL, 'someothervar defined');
exec sql else;
exec sql insert into test values (NULL, 'someothervar not defined');
exec sql endif;
exec sql define NUMBER 29;
exec sql select INSERTNULL, NUMBER::text || '-' || STR INTO :i, :s;
printf("i: %d, s: %s\n", i, s);
exec sql undef STR;
exec sql ifndef STR;
exec sql insert into test values (NUMBER, 'no string');
exec sql endif;
exec sql define TZVAR; /* no value */
exec sql define TZVAR 'UTC';
exec sql ifndef TZVAR;
exec sql SET TIMEZONE TO 'GMT';
exec sql elif TZNAME;
exec sql SET TIMEZONE TO TZNAME;
exec sql else;
exec sql SET TIMEZONE TO TZVAR;
exec sql endif;
/* test handling of a macro defined on the command line */
exec sql select CMDLINESYM INTO :i;
printf("original CMDLINESYM: %d\n", i);
exec sql define CMDLINESYM 42;
exec sql select CMDLINESYM INTO :i;
printf("redefined CMDLINESYM: %d\n", i);
exec sql define CMDLINESYM 43;
exec sql select CMDLINESYM INTO :i;
printf("redefined CMDLINESYM: %d\n", i);
exec sql undef CMDLINESYM;
exec sql ifdef CMDLINESYM;
exec sql insert into test values (NUMBER, 'no string');
exec sql endif;
/* this macro should not have carried over from define_prelim.pgc */
exec sql ifdef NONCMDLINESYM;
exec sql insert into test values (NUMBER, 'no string');
exec sql endif;
exec sql disconnect;
return 0;
}