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

Commit23382c4

Browse files
committed
Clean up code for widget_in() and widget_out().
Given syntactically wrong input, widget_in() could call atof() with anindeterminate pointer argument, typically leading to a crash; or if itdidn't do that, it might return a NULL pointer, which again would leadto a crash since old-style C functions aren't supposed to do thingsthat way. Fix that by correcting the off-by-one syntax test andthrowing a proper error rather than just returning NULL.Also, since widget_in and widget_out have been marked STRICT for along time, their tests for null inputs are just dead code; remove 'em.In the oldest branches, also improve widget_out to use snprintf notsprintf, just to be sure.In passing, get rid of a long-since-useless sprintf into a local bufferthat nothing further is done with, and make some other minor codingstyle cleanups.In the intended regression-testing usage of these functions, none ofthis is very significant; but if the regression test database wereleft around in a production installation, these bugs could amountto a minor security hazard.Piotr Stefaniak, Michael Paquier, and Tom Lane
1 parentf2c6804 commit23382c4

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

‎src/test/regress/regress.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,25 +244,27 @@ WIDGET *
244244
widget_in(char*str)
245245
{
246246
char*p,
247-
*coord[NARGS],
248-
buf2[1000];
247+
*coord[NARGS];
249248
inti;
250249
WIDGET*result;
251250

252-
if (str==NULL)
253-
returnNULL;
254251
for (i=0,p=str;*p&&i<NARGS&&*p!=RDELIM;p++)
255-
if (*p==','|| (*p==LDELIM&& !i))
252+
{
253+
if (*p==DELIM|| (*p==LDELIM&&i==0))
256254
coord[i++]=p+1;
257-
if (i<NARGS-1)
258-
returnNULL;
255+
}
256+
257+
if (i<NARGS)
258+
ereport(ERROR,
259+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
260+
errmsg("invalid input syntax for type widget: \"%s\"",
261+
str)));
262+
259263
result= (WIDGET*)palloc(sizeof(WIDGET));
260264
result->center.x=atof(coord[0]);
261265
result->center.y=atof(coord[1]);
262266
result->radius=atof(coord[2]);
263267

264-
snprintf(buf2,sizeof(buf2),"widget_in: read (%f, %f, %f)\n",
265-
result->center.x,result->center.y,result->radius);
266268
returnresult;
267269
}
268270

@@ -271,12 +273,9 @@ widget_out(WIDGET * widget)
271273
{
272274
char*result;
273275

274-
if (widget==NULL)
275-
returnNULL;
276-
277-
result= (char*)palloc(60);
278-
sprintf(result,"(%g,%g,%g)",
279-
widget->center.x,widget->center.y,widget->radius);
276+
result= (char*)palloc(100);
277+
snprintf(result,100,"(%g,%g,%g)",
278+
widget->center.x,widget->center.y,widget->radius);
280279
returnresult;
281280
}
282281

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp