diff options
author | Neil Conway <neilc@samurai.com> | 2004-03-11 02:11:14 +0000 |
---|---|---|
committer | Neil Conway <neilc@samurai.com> | 2004-03-11 02:11:14 +0000 |
commit | e2ded829f6b672529d072620e43de65466286b59 (patch) | |
tree | 9ffc21ade136f5e667fb41dbd2e8b1c56436b3e5 /src/backend/utils/adt/numutils.c | |
parent | 0b86ade1c2f3dcd2407e535baad1654e65252316 (diff) | |
download | postgresql-e2ded829f6b672529d072620e43de65466286b59.tar.gz postgresql-e2ded829f6b672529d072620e43de65466286b59.zip |
Revise int2/int4/int8/float4/float8 input routines to allow for
any amount of leading or trailing whitespace (where "whitespace"
is defined by isspace()). This is for SQL conformance, as well
as consistency with other numeric types (e.g. oid, numeric).
Also refactor pg_atoi() to avoid looking at errno where not
necessary, and add a bunch of regression tests for the input
to these types.
Diffstat (limited to 'src/backend/utils/adt/numutils.c')
-rw-r--r-- | src/backend/utils/adt/numutils.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/backend/utils/adt/numutils.c b/src/backend/utils/adt/numutils.c index 20227884ed1..17961017faf 100644 --- a/src/backend/utils/adt/numutils.c +++ b/src/backend/utils/adt/numutils.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.61 2004/02/18 00:01:33 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.62 2004/03/11 02:11:13 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -19,6 +19,7 @@ #include <errno.h> #include <math.h> #include <limits.h> +#include <ctype.h> #include "utils/builtins.h" @@ -45,10 +46,12 @@ /* * pg_atoi: convert string to integer * - * size is the sizeof() the desired integral result (1, 2, or 4 bytes). + * 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes). * - * c, if not 0, is the terminator character that may appear after the - * integer. If 0, the string must end after the integer. + * allows any number of leading or trailing whitespace characters. + * + * 'c' is the character that terminates the input string (after any + * number of whitespace characters). * * Unlike plain atoi(), this will throw ereport() upon bad input format or * overflow. @@ -57,7 +60,7 @@ int32 pg_atoi(char *s, int size, int c) { long l; - char *badp = NULL; + char *badp; /* * Some versions of strtol treat the empty string as an error, but @@ -74,17 +77,21 @@ pg_atoi(char *s, int size, int c) errno = 0; l = strtol(s, &badp, 10); - /* - * strtol() normally only sets ERANGE. On some systems it also may - * set EINVAL, which simply means it couldn't parse the input string. - * This is handled by the second "if" consistent across platforms. - */ - if (errno && errno != ERANGE && errno != EINVAL) + /* We made no progress parsing the string, so bail out */ + if (s == badp) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for integer: \"%s\"", s))); - if (badp && *badp && *badp != c) + + /* + * Skip any trailing whitespace; if anything but whitespace + * remains before the terminating character, bail out + */ + while (*badp != c && isspace(*badp)) + badp++; + + if (*badp != c) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for integer: \"%s\"", |