Skip to content

Commit b7050e2

Browse files
committed
Fix possible omission of variable storage markers in ECPG.
The ECPG preprocessor converted code such as static varchar str1[10], str2[20], str3[30]; into static struct varchar_1 { int len; char arr[ 10 ]; } str1 ; struct varchar_2 { int len; char arr[ 20 ]; } str2 ; struct varchar_3 { int len; char arr[ 30 ]; } str3 ; thus losing the storage attribute for the later variables. Repeat the declaration for each such variable. (Note that this occurred only for variables declared "varchar" or "bytea", which may help explain how it escaped detection for so long.) Andrey Sokolov Discussion: https://postgr.es/m/942241662288242@mail.yandex.ru
1 parent f734857 commit b7050e2

File tree

6 files changed

+151
-123
lines changed

6 files changed

+151
-123
lines changed

src/interfaces/ecpg/preproc/ecpg.trailer

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,10 @@ type_declaration: S_TYPEDEF
476476
$$ = mm_strdup("");
477477
};
478478

479-
var_declaration: storage_declaration
480-
var_type
479+
var_declaration:
480+
storage_declaration var_type
481481
{
482+
actual_type[struct_level].type_storage = $1;
482483
actual_type[struct_level].type_enum = $2.type_enum;
483484
actual_type[struct_level].type_str = $2.type_str;
484485
actual_type[struct_level].type_dimension = $2.type_dimension;
@@ -493,6 +494,7 @@ var_declaration: storage_declaration
493494
}
494495
| var_type
495496
{
497+
actual_type[struct_level].type_storage = EMPTY;
496498
actual_type[struct_level].type_enum = $1.type_enum;
497499
actual_type[struct_level].type_str = $1.type_str;
498500
actual_type[struct_level].type_dimension = $1.type_dimension;
@@ -949,7 +951,7 @@ variable_list: variable
949951
| variable_list ',' variable
950952
{
951953
if (actual_type[struct_level].type_enum == ECPGt_varchar || actual_type[struct_level].type_enum == ECPGt_bytea)
952-
$$ = cat_str(3, $1, mm_strdup(";"), $3);
954+
$$ = cat_str(4, $1, mm_strdup(";"), mm_strdup(actual_type[struct_level].type_storage), $3);
953955
else
954956
$$ = cat_str(3, $1, mm_strdup(","), $3);
955957
}

src/interfaces/ecpg/preproc/type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct exec
114114

115115
struct this_type
116116
{
117+
char *type_storage;
117118
enum ECPGttype type_enum;
118119
char *type_str;
119120
char *type_dimension;

src/interfaces/ecpg/test/expected/preproc-variable.c

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ main (void)
7171

7272

7373

74+
75+
7476

7577
#line 27 "variable.pgc"
7678
struct personal_struct {
@@ -98,27 +100,33 @@ main (void)
98100
} ; struct t2 {
99101
#line 32 "variable.pgc"
100102
struct varchar_3 { int len; char arr[ BUFFERSIZ ]; } name ;
101-
} ;/* exec sql end declare section */
103+
} ;
102104
#line 33 "variable.pgc"
105+
static struct varchar_4 { int len; char arr[ 50 ]; } vc1 ; static struct varchar_5 { int len; char arr[ 50 ]; } vc2 ; static struct varchar_6 { int len; char arr[ 255 ]; } vc3 ;
106+
107+
#line 34 "variable.pgc"
108+
static int i1 , i2 , i3 ;
109+
/* exec sql end declare section */
110+
#line 35 "variable.pgc"
103111

104112

105113

106-
#line 35 "variable.pgc"
114+
#line 37 "variable.pgc"
107115
char * married = NULL ;
108116

109-
#line 35 "variable.pgc"
117+
#line 37 "variable.pgc"
110118

111119

112-
#line 36 "variable.pgc"
120+
#line 38 "variable.pgc"
113121
long ind_married ;
114122

115-
#line 36 "variable.pgc"
123+
#line 38 "variable.pgc"
116124

117125

118-
#line 37 "variable.pgc"
126+
#line 39 "variable.pgc"
119127
ind children ;
120128

121-
#line 37 "variable.pgc"
129+
#line 39 "variable.pgc"
122130

123131
int loopcount;
124132
char msg[128];
@@ -127,78 +135,78 @@ main (void)
127135

128136
strcpy(msg, "connect");
129137
{ ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
130-
#line 44 "variable.pgc"
138+
#line 46 "variable.pgc"
131139

132140
if (sqlca.sqlcode < 0) exit (1);}
133-
#line 44 "variable.pgc"
141+
#line 46 "variable.pgc"
134142

135143

136144
strcpy(msg, "set");
137145
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
138-
#line 47 "variable.pgc"
146+
#line 49 "variable.pgc"
139147

140148
if (sqlca.sqlcode < 0) exit (1);}
141-
#line 47 "variable.pgc"
149+
#line 49 "variable.pgc"
142150

143151

144152
strcpy(msg, "create");
145153
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table family ( name char ( 8 ) , born integer , age smallint , married date , children integer )", ECPGt_EOIT, ECPGt_EORT);
146-
#line 50 "variable.pgc"
154+
#line 52 "variable.pgc"
147155

148156
if (sqlca.sqlcode < 0) exit (1);}
149-
#line 50 "variable.pgc"
157+
#line 52 "variable.pgc"
150158

151159

152160
strcpy(msg, "insert");
153161
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , married , children ) values ( 'Mum' , '19870714' , 3 )", ECPGt_EOIT, ECPGt_EORT);
154-
#line 53 "variable.pgc"
162+
#line 55 "variable.pgc"
155163

156164
if (sqlca.sqlcode < 0) exit (1);}
157-
#line 53 "variable.pgc"
165+
#line 55 "variable.pgc"
158166

159167
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , born , married , children ) values ( 'Dad' , '19610721' , '19870714' , 3 )", ECPGt_EOIT, ECPGt_EORT);
160-
#line 54 "variable.pgc"
168+
#line 56 "variable.pgc"
161169

162170
if (sqlca.sqlcode < 0) exit (1);}
163-
#line 54 "variable.pgc"
171+
#line 56 "variable.pgc"
164172

165173
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , age ) values ( 'Child 1' , 16 )", ECPGt_EOIT, ECPGt_EORT);
166-
#line 55 "variable.pgc"
174+
#line 57 "variable.pgc"
167175

168176
if (sqlca.sqlcode < 0) exit (1);}
169-
#line 55 "variable.pgc"
177+
#line 57 "variable.pgc"
170178

171179
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , age ) values ( 'Child 2' , 14 )", ECPGt_EOIT, ECPGt_EORT);
172-
#line 56 "variable.pgc"
180+
#line 58 "variable.pgc"
173181

174182
if (sqlca.sqlcode < 0) exit (1);}
175-
#line 56 "variable.pgc"
183+
#line 58 "variable.pgc"
176184

177185
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , age ) values ( 'Child 3' , 9 )", ECPGt_EOIT, ECPGt_EORT);
178-
#line 57 "variable.pgc"
186+
#line 59 "variable.pgc"
179187

180188
if (sqlca.sqlcode < 0) exit (1);}
181-
#line 57 "variable.pgc"
189+
#line 59 "variable.pgc"
182190

183191

184192
strcpy(msg, "commit");
185193
{ ECPGtrans(__LINE__, NULL, "commit");
186-
#line 60 "variable.pgc"
194+
#line 62 "variable.pgc"
187195

188196
if (sqlca.sqlcode < 0) exit (1);}
189-
#line 60 "variable.pgc"
197+
#line 62 "variable.pgc"
190198

191199

192200
strcpy(msg, "open");
193201
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare cur cursor for select name , born , age , married , children from family", ECPGt_EOIT, ECPGt_EORT);
194-
#line 63 "variable.pgc"
202+
#line 65 "variable.pgc"
195203

196204
if (sqlca.sqlcode < 0) exit (1);}
197-
#line 63 "variable.pgc"
205+
#line 65 "variable.pgc"
198206

199207

200208
/* exec sql whenever not found break ; */
201-
#line 65 "variable.pgc"
209+
#line 67 "variable.pgc"
202210

203211

204212
p=&personal;
@@ -217,13 +225,13 @@ if (sqlca.sqlcode < 0) exit (1);}
217225
ECPGt_long,&(ind_married),(long)1,(long)1,sizeof(long),
218226
ECPGt_int,&(children.integer),(long)1,(long)1,sizeof(int),
219227
ECPGt_short,&(ind_children.smallint),(long)1,(long)1,sizeof(short), ECPGt_EORT);
220-
#line 72 "variable.pgc"
228+
#line 74 "variable.pgc"
221229

222230
if (sqlca.sqlcode == ECPG_NOT_FOUND) break;
223-
#line 72 "variable.pgc"
231+
#line 74 "variable.pgc"
224232

225233
if (sqlca.sqlcode < 0) exit (1);}
226-
#line 72 "variable.pgc"
234+
#line 74 "variable.pgc"
227235

228236
printf("%8.8s", personal.name.arr);
229237
if (i->ind_birth.born >= 0)
@@ -242,35 +250,42 @@ if (sqlca.sqlcode < 0) exit (1);}
242250

243251
strcpy(msg, "close");
244252
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close cur", ECPGt_EOIT, ECPGt_EORT);
245-
#line 89 "variable.pgc"
253+
#line 91 "variable.pgc"
246254

247255
if (sqlca.sqlcode < 0) exit (1);}
248-
#line 89 "variable.pgc"
256+
#line 91 "variable.pgc"
249257

250258

251259
strcpy(msg, "drop");
252260
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table family", ECPGt_EOIT, ECPGt_EORT);
253-
#line 92 "variable.pgc"
261+
#line 94 "variable.pgc"
254262

255263
if (sqlca.sqlcode < 0) exit (1);}
256-
#line 92 "variable.pgc"
264+
#line 94 "variable.pgc"
257265

258266

259267
strcpy(msg, "commit");
260268
{ ECPGtrans(__LINE__, NULL, "commit");
261-
#line 95 "variable.pgc"
269+
#line 97 "variable.pgc"
262270

263271
if (sqlca.sqlcode < 0) exit (1);}
264-
#line 95 "variable.pgc"
272+
#line 97 "variable.pgc"
265273

266274

267275
strcpy(msg, "disconnect");
268276
{ ECPGdisconnect(__LINE__, "CURRENT");
269-
#line 98 "variable.pgc"
277+
#line 100 "variable.pgc"
270278

271279
if (sqlca.sqlcode < 0) exit (1);}
272-
#line 98 "variable.pgc"
280+
#line 100 "variable.pgc"
281+
273282

283+
/* this just to silence unused-variable warnings: */
284+
vc1.len = vc2.len = vc3.len = 0;
285+
i1 = i2 = i3 = 0;
286+
printf("%d %d %d %d %d %d\n",
287+
vc1.len, vc2.len, vc3.len,
288+
i1, i2, i3);
274289

275290
return 0;
276291
}

0 commit comments

Comments
 (0)