Skip to content

Commit 23382c4

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

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
int i;
250249
WIDGET *result;
251250

252-
if (str == NULL)
253-
return NULL;
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-
return NULL;
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
return result;
267269
}
268270

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

274-
if (widget == NULL)
275-
return NULL;
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
return result;
281280
}
282281

0 commit comments

Comments
 (0)