8000 Clean up code for widget_in() and widget_out(). · qadahtm/postgres@fe25785 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe25785

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 e8808f3 commit fe25785

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
@@ -243,25 +243,27 @@ WIDGET *
243243
widget_in(char *str)
244244
{
245245
char *p,
246-
*coord[NARGS],
247-
buf2[1000];
246+
*coord[NARGS];
248247
int i;
249248
WIDGET *result;
250249

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

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

@@ -270,12 +272,9 @@ widget_out(WIDGET * widget)
270272
{
271273
char *result;
272274

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

0 commit comments

Comments
 (0)
0