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

Commit1603dec

Browse files
committed
Don't use abort(3) in libpq's fe-print.c.
Causing a core dump on out-of-memory seems pretty unfriendly,and surely is far outside the expected behavior of a general-purposelibrary. Just print an error message (as we did already) and return.These functions unfortunately don't have an error return convention,but code using them is probably just looking for a quick-n-dirtyprint method and wouldn't bother to check anyway.Although these functions are semi-deprecated, it still seemsappropriate to back-patch this. In passing, also back-patchb90e6ce, just to reduce cosmetic differences between thebranches.Discussion:https://postgr.es/m/3122443.1624735363@sss.pgh.pa.us
1 parentbe567de commit1603dec

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

‎src/interfaces/libpq/fe-print.c

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#definePQmblenBounded(s,e) strnlen(s, PQmblen(s, e))
4040

41-
staticvoiddo_field(constPQprintOpt*po,constPGresult*res,
41+
staticbooldo_field(constPQprintOpt*po,constPGresult*res,
4242
constinti,constintj,constintfs_len,
4343
char**fields,
4444
constintnFields,constchar**fieldNames,
@@ -81,12 +81,12 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
8181
unsignedchar*fieldNotNum=NULL;
8282
char*border=NULL;
8383
char**fields=NULL;
84-
constchar**fieldNames;
84+
constchar**fieldNames=NULL;
8585
intfieldMaxLen=0;
8686
intnumFieldName;
8787
intfs_len=strlen(po->fieldSep);
8888
inttotal_line_length=0;
89-
intusePipe=0;
89+
boolusePipe=false;
9090
char*pagerenv;
9191

9292
#if defined(ENABLE_THREAD_SAFETY)&& !defined(WIN32)
@@ -109,20 +109,13 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
109109
#endif
110110

111111
nTups=PQntuples(res);
112-
if (!(fieldNames= (constchar**)calloc(nFields,sizeof(char*))))
112+
fieldNames= (constchar**)calloc(nFields,sizeof(char*));
113+
fieldNotNum= (unsignedchar*)calloc(nFields,1);
114+
fieldMax= (int*)calloc(nFields,sizeof(int));
115+
if (!fieldNames|| !fieldNotNum|| !fieldMax)
113116
{
114117
fprintf(stderr,libpq_gettext("out of memory\n"));
115-
abort();
116-
}
117-
if (!(fieldNotNum= (unsignedchar*)calloc(nFields,1)))
118-
{
119-
fprintf(stderr,libpq_gettext("out of memory\n"));
120-
abort();
121-
}
122-
if (!(fieldMax= (int*)calloc(nFields,sizeof(int))))
123-
{
124-
fprintf(stderr,libpq_gettext("out of memory\n"));
125-
abort();
118+
gotoexit;
126119
}
127120
for (numFieldName=0;
128121
po->fieldName&&po->fieldName[numFieldName];
@@ -191,7 +184,7 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
191184
fout=popen(pagerenv,"w");
192185
if (fout)
193186
{
194-
usePipe=1;
187+
usePipe=true;
195188
#ifndefWIN32
196189
#ifdefENABLE_THREAD_SAFETY
197190
if (pq_block_sigpipe(&osigset,&sigpipe_pending)==0)
@@ -208,10 +201,12 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
208201

209202
if (!po->expanded&& (po->align||po->html3))
210203
{
211-
if (!(fields= (char**)calloc(nFields* (nTups+1),sizeof(char*))))
204+
fields= (char**)calloc((size_t)nTups+1,
205+
nFields*sizeof(char*));
206+
if (!fields)
212207
{
213208
fprintf(stderr,libpq_gettext("out of memory\n"));
214-
abort();
209+
gotoexit;
215210
}
216211
}
217212
elseif (po->header&& !po->html3)
@@ -265,9 +260,12 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
265260
fprintf(fout,libpq_gettext("-- RECORD %d --\n"),i);
266261
}
267262
for (j=0;j<nFields;j++)
268-
do_field(po,res,i,j,fs_len,fields,nFields,
269-
fieldNames,fieldNotNum,
270-
fieldMax,fieldMaxLen,fout);
263+
{
264+
if (!do_field(po,res,i,j,fs_len,fields,nFields,
265+
fieldNames,fieldNotNum,
266+
fieldMax,fieldMaxLen,fout))
267+
gotoexit;
268+
}
271269
if (po->html3&&po->expanded)
272270
fputs("</table>\n",fout);
273271
}
@@ -298,18 +296,34 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
298296
for (i=0;i<nTups;i++)
299297
output_row(fout,po,nFields,fields,
300298
fieldNotNum,fieldMax,border,i);
301-
free(fields);
302-
if (border)
303-
free(border);
304299
}
305300
if (po->header&& !po->html3)
306301
fprintf(fout,"(%d row%s)\n\n",PQntuples(res),
307302
(PQntuples(res)==1) ?"" :"s");
308303
if (po->html3&& !po->expanded)
309304
fputs("</table>\n",fout);
310-
free(fieldMax);
311-
free(fieldNotNum);
312-
free((void*)fieldNames);
305+
306+
exit:
307+
if (fieldMax)
308+
free(fieldMax);
309+
if (fieldNotNum)
310+
free(fieldNotNum);
311+
if (border)
312+
free(border);
313+
if (fields)
314+
{
315+
/* if calloc succeeded, this shouldn't overflow size_t */
316+
size_tnumfields= ((size_t)nTups+1)* (size_t)nFields;
317+
318+
while (numfields-->0)
319+
{
320+
if (fields[numfields])
321+
free(fields[numfields]);
322+
}
323+
free(fields);
324+
}
325+
if (fieldNames)
326+
free((void*)fieldNames);
313327
if (usePipe)
314328
{
315329
#ifdefWIN32
@@ -330,7 +344,7 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
330344
}
331345

332346

333-
staticvoid
347+
staticbool
334348
do_field(constPQprintOpt*po,constPGresult*res,
335349
constinti,constintj,constintfs_len,
336350
char**fields,
@@ -398,7 +412,7 @@ do_field(const PQprintOpt *po, const PGresult *res,
398412
if (!(fields[i*nFields+j]= (char*)malloc(plen+1)))
399413
{
400414
fprintf(stderr,libpq_gettext("out of memory\n"));
401-
abort();
415+
return false;
402416
}
403417
strcpy(fields[i*nFields+j],pval);
404418
}
@@ -441,6 +455,7 @@ do_field(const PQprintOpt *po, const PGresult *res,
441455
}
442456
}
443457
}
458+
return true;
444459
}
445460

446461

@@ -468,7 +483,7 @@ do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax,
468483
if (!border)
469484
{
470485
fprintf(stderr,libpq_gettext("out of memory\n"));
471-
abort();
486+
returnNULL;
472487
}
473488
p=border;
474489
if (po->standard)
@@ -559,8 +574,6 @@ output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields,
559574
if (po->standard||field_index+1<nFields)
560575
fputs(po->fieldSep,fout);
561576
}
562-
if (p)
563-
free(p);
564577
}
565578
if (po->html3)
566579
fputs("</tr>",fout);
@@ -610,7 +623,7 @@ PQdisplayTuples(const PGresult *res,
610623
if (!fLength)
611624
{
612625
fprintf(stderr,libpq_gettext("out of memory\n"));
613-
abort();
626+
return;
614627
}
615628

616629
for (j=0;j<nFields;j++)
@@ -708,7 +721,7 @@ PQprintTuples(const PGresult *res,
708721
if (!tborder)
709722
{
710723
fprintf(stderr,libpq_gettext("out of memory\n"));
711-
abort();
724+
return;
712725
}
713726
for (i=0;i<width;i++)
714727
tborder[i]='-';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp