Skip to content

Commit acbdda4

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 831c22b commit acbdda4

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
@@ -237,34 +237,33 @@ WIDGET *
237237
widget_in(char *str)
238238
{
239239
char *p,
240-
*coord[NARGS],
241-
buf2[1000];
240+
*coord[NARGS];
242241
int i;
243242
WIDGET *result;
244243

245-
if (str == NULL)
246-
return NULL;
247244
for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
248-
if (*p == ',' || (*p == LDELIM && !i))
245+
{
246+
if (*p == DELIM || (*p == LDELIM && i == 0))
249247
coord[i++] = p + 1;
250-
if (i < NARGS - 1)
251-
return NULL;
248+
}
249+
250+
if (i < NARGS)
251+
ereport(ERROR,
252+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
253+
errmsg("invalid input syntax for type widget: \"%s\"",
254+
str)));
255+
252256
result = (WIDGET *) palloc(sizeof(WIDGET));
253257
result->center.x = atof(coord[0]);
254258
result->center.y = atof(coord[1]);
255259
result->radius = atof(coord[2]);
256260

257-
snprintf(buf2, sizeof(buf2), "widget_in: read (%f, %f, %f)\n",
258-
result->center.x, result->center.y, result->radius);
259261
return result;
260262
}
261263

262264
char *
263265
widget_out(WIDGET *widget)
264266
{
265-
if (widget == NULL)
266-
return NULL;
267-
268267
return psprintf("(%g,%g,%g)",
269268
widget->center.x, widget->center.y, widget->radius);
270269
}

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