aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/numutils.c
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>1998-09-12 16:04:35 +0000
committerBruce Momjian <bruce@momjian.us>1998-09-12 16:04:35 +0000
commit786b1802c801eba347a42675ca0a09228b6588a1 (patch)
tree4b77738dfed3759f08d7f28e13ddbe919281f595 /src/backend/utils/adt/numutils.c
parent5f7fb677893827bdd09eaac7f5148c8261e0af04 (diff)
downloadpostgresql-786b1802c801eba347a42675ca0a09228b6588a1.tar.gz
postgresql-786b1802c801eba347a42675ca0a09228b6588a1.zip
The pg_atoi() function uses strtol() to convert the string to numbers. Some
implementations of strtol() treat empty strings ("") as invalid arguments while others convert this (erroneously, IHMO) to zero (0). Assuming that the expected behaviour of pg_atoi() is to return 0 if it is passed an empty string, I am supplying the following patch to explictly check for an empty string in pg_atoi() and return 0 if the string is empty. The patch will also trap a NULL character pointer being passed to pg_atoi() and will use elog() to print out an error message if the input char pointer is NULL. Billy G. Allie
Diffstat (limited to 'src/backend/utils/adt/numutils.c')
-rw-r--r--src/backend/utils/adt/numutils.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/backend/utils/adt/numutils.c b/src/backend/utils/adt/numutils.c
index b30b2fe557b..2dd6e5db9e1 100644
--- a/src/backend/utils/adt/numutils.c
+++ b/src/backend/utils/adt/numutils.c
@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.26 1998/09/01 04:32:43 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.27 1998/09/12 16:04:35 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -55,8 +55,19 @@ pg_atoi(char *s, int size, int c)
Assert(s);
- errno = 0;
- l = strtol(s, &badp, 10);
+ errno = 0;
+
+ /*
+ * Some versions of strtol treat the empty string as an error. This
+ * code will explicitly return 0 for an empty string.
+ */
+
+ if (s == (char *)NULL)
+ elog(ERROR, "pg_atoi: NULL pointer!");
+ else if (*s == 0)
+ l = (long)0;
+ else
+ l = strtol(s, &badp, 10);
if (errno) /* strtol must set ERANGE */
elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
if (badp && *badp && (*badp != c))