Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4631646

Browse files
committed
Fix assorted bugs in ecpg's macro mechanism.
The code associated with EXEC SQL DEFINE was unreadable and full ofbugs, notably:* It'd attempt to free a non-malloced string if the ecpg programtries 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 wouldchange the state visible to the next file, when multiple files arespecified on the command line. (While possibly that could have beenan intentional choice, the code clearly intends to revert to theoriginal macro state; it's just failing to consider this interaction.)* Missing "break" in defining a new macro meant that redefinitionof an existing name would cause an extra entry to be added to thedefinition list. While not immediately harmful, a subsequent undefwould result in the prior entry becoming visible again.* The interactions with input buffering are subtle and were entirelyundocumented.It's not that surprising that we hadn't noticed these bugs,because there was no test coverage at all of either the -Dcommand line switch or multiple input files. This patch addssuch coverage (in a rather hacky way I guess).In addition to the code bugs, the user documentation was confusedabout whether the -D switch defines a C macro or an ecpg one, andit 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
1 parentab24022 commit4631646

File tree

11 files changed

+288
-80
lines changed

11 files changed

+288
-80
lines changed

‎doc/src/sgml/ecpg.sgml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5755,6 +5755,14 @@ EXEC SQL UPDATE Tbl SET col = MYNUMBER;
57555755
embedded SQL query because in this case the embedded SQL precompiler is not
57565756
able to see this declaration.
57575757
</para>
5758+
5759+
<para>
5760+
If multiple input files are named on the <command>ecpg</command>
5761+
preprocessor's command line, the effects of <literal>EXEC SQL
5762+
DEFINE</literal> and <literal>EXEC SQL UNDEF</literal> do not carry
5763+
across files: each file starts with only the symbols defined
5764+
by <option>-D</option> switches on the command line.
5765+
</para>
57585766
</sect2>
57595767

57605768
<sect2 id="ecpg-ifdef">

‎doc/src/sgml/ref/ecpg-ref.sgml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ PostgreSQL documentation
9393
</varlistentry>
9494

9595
<varlistentry>
96-
<term><option>-D <replaceable>symbol</replaceable></option></term>
96+
<term><option>-D <replaceable>symbol</replaceable>[=<replaceable>value</replaceable>]</option></term>
9797
<listitem>
9898
<para>
99-
Define a C preprocessor symbol.
99+
Define a preprocessor symbol, equivalently to the <command>EXEC SQL
100+
DEFINE</command> directive. If no <replaceable>value</replaceable> is
101+
specified, the symbol is defined with the value <literal>1</literal>.
100102
</para>
101103
</listitem>
102104
</varlistentry>

‎src/interfaces/ecpg/preproc/ecpg.c

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,46 @@ add_include_path(char *path)
8282
}
8383
}
8484

85+
/*
86+
* Process a command line -D switch
87+
*/
8588
staticvoid
8689
add_preprocessor_define(char*define)
8790
{
88-
struct_defines*pd=defines;
89-
char*ptr,
90-
*define_copy=mm_strdup(define);
91+
/* copy the argument to avoid relying on argv storage */
92+
char*define_copy=mm_strdup(define);
93+
char*ptr;
94+
struct_defines*newdef;
9195

92-
defines=mm_alloc(sizeof(struct_defines));
96+
newdef=mm_alloc(sizeof(struct_defines));
9397

9498
/* look for = sign */
9599
ptr=strchr(define_copy,'=');
96100
if (ptr!=NULL)
97101
{
102+
/* symbol has a value */
98103
char*tmp;
99104

100-
/*symbol has a value */
101-
for (tmp=ptr-1;*tmp==' ';tmp--);
105+
/*strip any spaces between name and '=' */
106+
for (tmp=ptr-1;tmp >=define_copy&&*tmp==' ';tmp--);
102107
tmp[1]='\0';
103-
defines->olddef=define_copy;
104-
defines->newdef=ptr+1;
108+
109+
/*
110+
* Note we don't bother to separately malloc cmdvalue; it will never
111+
* be freed so that's not necessary.
112+
*/
113+
newdef->cmdvalue=ptr+1;
105114
}
106115
else
107116
{
108-
defines->olddef=define_copy;
109-
defines->newdef=mm_strdup("1");
117+
/* define it as "1"; again no need to malloc it */
118+
newdef->cmdvalue="1";
110119
}
111-
defines->pertinent= true;
112-
defines->used=NULL;
113-
defines->next=pd;
120+
newdef->name=define_copy;
121+
newdef->value=mm_strdup(newdef->cmdvalue);
122+
newdef->used=NULL;
123+
newdef->next=defines;
124+
defines=newdef;
114125
}
115126

116127
#defineECPG_GETOPT_LONG_REGRESSION1
@@ -347,6 +358,8 @@ main(int argc, char *const argv[])
347358
{
348359
structcursor*ptr;
349360
struct_defines*defptr;
361+
struct_defines*prevdefptr;
362+
struct_defines*nextdefptr;
350363
structtypedefs*typeptr;
351364
structdeclared_list*list;
352365

@@ -384,28 +397,28 @@ main(int argc, char *const argv[])
384397
free(this);
385398
}
386399

387-
/* remove non-pertinent old defines as well */
388-
while (defines&& !defines->pertinent)
400+
/* restore defines to their command-line state */
401+
prevdefptr=NULL;
402+
for (defptr=defines;defptr!=NULL;defptr=nextdefptr)
389403
{
390-
defptr=defines;
391-
defines=defines->next;
392-
393-
free(defptr->newdef);
394-
free(defptr->olddef);
395-
free(defptr);
396-
}
397-
398-
for (defptr=defines;defptr!=NULL;defptr=defptr->next)
399-
{
400-
struct_defines*this=defptr->next;
401-
402-
if (this&& !this->pertinent)
404+
nextdefptr=defptr->next;
405+
if (defptr->cmdvalue!=NULL)
403406
{
404-
defptr->next=this->next;
405-
406-
free(this->newdef);
407-
free(this->olddef);
408-
free(this);
407+
/* keep it, resetting the value */
408+
free(defptr->value);
409+
defptr->value=mm_strdup(defptr->cmdvalue);
410+
prevdefptr=defptr;
411+
}
412+
else
413+
{
414+
/* remove it */
415+
if (prevdefptr!=NULL)
416+
prevdefptr->next=nextdefptr;
417+
else
418+
defines=nextdefptr;
419+
free(defptr->name);
420+
free(defptr->value);
421+
free(defptr);
409422
}
410423
}
411424

‎src/interfaces/ecpg/preproc/pgc.l

Lines changed: 89 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,21 @@ char *token_start;
6969
staticint state_before_str_start;
7070
staticint state_before_str_stop;
7171

72-
struct_yy_buffer
72+
/*
73+
* State for handling include files and macro expansion. We use a new
74+
* flex input buffer for each level of include or macro, and create a
75+
* struct _yy_buffer to remember the previous level. There is not a struct
76+
* for the currently active input source; that state is kept in the global
77+
* variables YY_CURRENT_BUFFER, yylineno, and input_filename.
78+
*/
79+
staticstruct_yy_buffer
7380
{
7481
YY_BUFFER_STATEbuffer;
7582
longlineno;
7683
char *filename;
7784
struct_yy_buffer *next;
7885
} *yy_buffer =NULL;
7986

80-
staticchar *old;
81-
8287
/*
8388
* Vars for handling ifdef/elif/endif constructs. preproc_tos is the current
8489
* nesting depth of such constructs, and stacked_if_value[preproc_tos] is the
@@ -426,6 +431,8 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
426431

427432
%{
428433
/* code to execute during start of each call of yylex()*/
434+
char *newdefsymbol =NULL;
435+
429436
token_start =NULL;
430437
%}
431438

@@ -961,6 +968,7 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
961968
}
962969

963970
{identifier}{
971+
/* First check to see if it's a define symbol to expand */
964972
if (!isdefine())
965973
{
966974
intkwvalue;
@@ -1153,17 +1161,23 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
11531161
yytext[i+1] ='\0';
11541162

11551163

1156-
for (ptr = defines; ptr !=NULL; ptr2 = ptr, ptr = ptr->next)
1164+
/* Find and unset any matching define; should be only 1 */
1165+
for (ptr = defines; ptr; ptr2 = ptr, ptr = ptr->next)
11571166
{
1158-
if (strcmp(yytext, ptr->olddef) ==0)
1167+
if (strcmp(yytext, ptr->name) ==0)
11591168
{
1160-
if (ptr2 ==NULL)
1161-
defines = ptr->next;
1162-
else
1163-
ptr2->next = ptr->next;
1164-
free(ptr->newdef);
1165-
free(ptr->olddef);
1166-
free(ptr);
1169+
free(ptr->value);
1170+
ptr->value =NULL;
1171+
/* We cannot forget it if there's a cmdvalue */
1172+
if (ptr->cmdvalue ==NULL)
1173+
{
1174+
if (ptr2 ==NULL)
1175+
defines = ptr->next;
1176+
else
1177+
ptr2->next = ptr->next;
1178+
free(ptr->name);
1179+
free(ptr);
1180+
}
11671181
break;
11681182
}
11691183
}
@@ -1368,11 +1382,17 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
13681382
;
13691383
yytext[i+1] ='\0';
13701384

1371-
for (defptr = defines;
1372-
defptr !=NULL &&
1373-
strcmp(yytext, defptr->olddef) !=0;
1374-
defptr = defptr->next)
1375-
/* skip */ ;
1385+
/* Does a definition exist? */
1386+
for (defptr = defines; defptr; defptr = defptr->next)
1387+
{
1388+
if (strcmp(yytext, defptr->name) ==0)
1389+
{
1390+
/* Found it, but is it currently undefined? */
1391+
if (defptr->value ==NULL)
1392+
defptr =NULL;/* pretend it's not found */
1393+
break;
1394+
}
1395+
}
13761396

13771397
this_active = (defptr ? ifcond : !ifcond);
13781398
stacked_if_value[preproc_tos].active =
@@ -1393,7 +1413,7 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
13931413
yyterminate();
13941414
}
13951415
<def_ident>{identifier} {
1396-
old =mm_strdup(yytext);
1416+
newdefsymbol =mm_strdup(yytext);
13971417
BEGIN(def);
13981418
startlit();
13991419
}
@@ -1402,26 +1422,31 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
14021422
yyterminate();
14031423
}
14041424
<def>{space}*";"{
1405-
struct_defines *ptr, *this;
1425+
struct_defines *ptr;
14061426

1427+
/* Does it already exist? */
14071428
for (ptr = defines; ptr !=NULL; ptr = ptr->next)
14081429
{
1409-
if (strcmp(old, ptr->olddef) ==0)
1410-
{
1411-
free(ptr->newdef);
1412-
ptr->newdef =mm_strdup(literalbuf);
1413-
}
1430+
if (strcmp(newdefsymbol, ptr->name) ==0)
1431+
{
1432+
free(ptr->value);
1433+
ptr->value =mm_strdup(literalbuf);
1434+
/* Don't leak newdefsymbol */
1435+
free(newdefsymbol);
1436+
break;
1437+
}
14141438
}
14151439
if (ptr ==NULL)
14161440
{
1417-
this = (struct_defines *)mm_alloc(sizeof(struct_defines));
1418-
1419-
/* initial definition */
1420-
this->olddef = old;
1421-
this->newdef =mm_strdup(literalbuf);
1422-
this->next = defines;
1423-
this->used =NULL;
1424-
defines =this;
1441+
/* Not present, make a new entry */
1442+
ptr = (struct_defines *)mm_alloc(sizeof(struct_defines));
1443+
1444+
ptr->name = newdefsymbol;
1445+
ptr->value =mm_strdup(literalbuf);
1446+
ptr->cmdvalue =NULL;
1447+
ptr->used =NULL;
1448+
ptr->next = defines;
1449+
defines = ptr;
14251450
}
14261451

14271452
BEGIN(C);
@@ -1438,6 +1463,7 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
14381463
<<EOF>>{
14391464
if (yy_buffer ==NULL)
14401465
{
1466+
/* No more input */
14411467
if ( preproc_tos >0 )
14421468
{
14431469
preproc_tos =0;
@@ -1447,16 +1473,20 @@ cppline{space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
14471473
}
14481474
else
14491475
{
1476+
/* Revert to previous input source */
14501477
struct_yy_buffer *yb = yy_buffer;
14511478
int i;
14521479
struct_defines *ptr;
14531480

1481+
/* Check to see if we are exiting a macro value */
14541482
for (ptr = defines; ptr; ptr = ptr->next)
1483+
{
14551484
if (ptr->used == yy_buffer)
14561485
{
14571486
ptr->used =NULL;
1458-
break;
1487+
break;/* there can't be multiple matches */
14591488
}
1489+
}
14601490

14611491
if (yyin !=NULL)
14621492
fclose(yyin);
@@ -1681,15 +1711,24 @@ ecpg_isspace(char ch)
16811711
returnfalse;
16821712
}
16831713

1684-
staticboolisdefine(void)
1714+
/*
1715+
* If yytext matches a define symbol, begin scanning the symbol's value
1716+
* and return true
1717+
*/
1718+
staticbool
1719+
isdefine(void)
16851720
{
16861721
struct_defines *ptr;
16871722

16881723
/* is it a define? */
16891724
for (ptr = defines; ptr; ptr = ptr->next)
16901725
{
1691-
if (strcmp(yytext, ptr->olddef) ==0 && ptr->used ==NULL)
1726+
/* notice we do not match anything being actively expanded */
1727+
if (strcmp(yytext, ptr->name) ==0 &&
1728+
ptr->value !=NULL &&
1729+
ptr->used ==NULL)
16921730
{
1731+
/* Save state associated with the current buffer */
16931732
struct_yy_buffer *yb;
16941733

16951734
yb =mm_alloc(sizeof(struct_yy_buffer));
@@ -1698,18 +1737,30 @@ static bool isdefine(void)
16981737
yb->lineno = yylineno;
16991738
yb->filename =mm_strdup(input_filename);
17001739
yb->next = yy_buffer;
1740+
yy_buffer = yb;
17011741

1702-
ptr->used = yy_buffer = yb;
1742+
/* Mark symbol as being actively expanded */
1743+
ptr->used = yb;
17031744

1704-
yy_scan_string(ptr->newdef);
1745+
/*
1746+
* We use yy_scan_string which will copy the value, so there's
1747+
* no need to worry about a possible undef happening while we
1748+
* are still scanning it.
1749+
*/
1750+
yy_scan_string(ptr->value);
17051751
returntrue;
17061752
}
17071753
}
17081754

17091755
returnfalse;
17101756
}
17111757

1712-
staticboolisinformixdefine(void)
1758+
/*
1759+
* Handle replacement of INFORMIX built-in defines. This works just
1760+
* like isdefine() except for the source of the string to scan.
1761+
*/
1762+
staticbool
1763+
isinformixdefine(void)
17131764
{
17141765
constchar *new =NULL;
17151766

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp