Skip to content

Commit 1cb63c7

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 b602842 commit 1cb63c7

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/test/regress/regress.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,34 +249,33 @@ WIDGET *
249249
widget_in(char *str)
250250
{
251251
char *p,
252-
*coord[NARGS],
253-
buf2[1000];
252+
*coord[NARGS];
254253
int i;
255254
WIDGET *result;
256255

257-
if (str == NULL)
258-
return NULL;
259256
for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
260-
if (*p == ',' || (*p == LDELIM && !i))
257+
{
258+
if (*p == DELIM || (*p == LDELIM && i == 0))
261259
coord[i++] = p + 1;
262-
if (i < NARGS - 1)
263-
return NULL;
260+
}
261+
262+
if (i < NARGS)
263+
ereport(ERROR,
264+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
265+
errmsg("invalid input syntax for type widget: \"%s\"",
266+
str)));
267+
264268
result = (WIDGET *) palloc(sizeof(WIDGET));
265269
result->center.x = atof(coord[0]);
266270
result->center.y = atof(coord[1]);
267271
result->radius = atof(coord[2]);
268272

269-
snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n",
270-
result->center.x, result->center.y, result->radius);
271273
return result;
272274
}
273275

274276
char *
275277
widget_out(WIDGET *widget)
276278
{
277-
if (widget == NULL)
278-
return NULL;
279-
280279
return psprintf("(%g,%g,%g)",
281280
widget->center.x, widget->center.y, widget->radius);
282281
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy