Rearrange code in pg_atoi() to avoid assuming that isspace() cannot

change errno.  No reported bugs here, but why take a chance?
This commit is contained in:
Tom Lane 2005-11-30 23:10:08 +00:00
parent bae3fefd4a
commit 164442fe7f

View File

@ -10,13 +10,12 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.69 2005/10/15 02:49:29 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.70 2005/11/30 23:10:08 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
#include <errno.h>
#include <math.h> #include <math.h>
#include <limits.h> #include <limits.h>
#include <ctype.h> #include <ctype.h>
@ -84,19 +83,6 @@ pg_atoi(char *s, int size, int c)
errmsg("invalid input syntax for integer: \"%s\"", errmsg("invalid input syntax for integer: \"%s\"",
s))); s)));
/*
* Skip any trailing whitespace; if anything but whitespace remains before
* the terminating character, bail out
*/
while (*badp && *badp != c && isspace((unsigned char) *badp))
badp++;
if (*badp && *badp != c)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for integer: \"%s\"",
s)));
switch (size) switch (size)
{ {
case sizeof(int32): case sizeof(int32):
@ -125,6 +111,20 @@ pg_atoi(char *s, int size, int c)
default: default:
elog(ERROR, "unsupported result size: %d", size); elog(ERROR, "unsupported result size: %d", size);
} }
/*
* Skip any trailing whitespace; if anything but whitespace remains before
* the terminating character, bail out
*/
while (*badp && *badp != c && isspace((unsigned char) *badp))
badp++;
if (*badp && *badp != c)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for integer: \"%s\"",
s)));
return (int32) l; return (int32) l;
} }