diff options
author | Michael Paquier <michael@paquier.xyz> | 2019-08-05 15:35:16 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2019-08-05 15:35:16 +0900 |
commit | a76cfba663ceab79b891cc81a5f941051755b3b0 (patch) | |
tree | 9aaaf52bc2d59eb507d4562ec5f9e6c039122809 /src/backend/utils/adt/float.c | |
parent | 05ba8370b8e4b5c8f3dd51986b9fdeb43fed5610 (diff) | |
download | postgresql-a76cfba663ceab79b891cc81a5f941051755b3b0.tar.gz postgresql-a76cfba663ceab79b891cc81a5f941051755b3b0.zip |
Add safeguards in LSN, numeric and float calculation for custom errors
Those data types use parsing and/or calculation wrapper routines which
can generate some generic error messages in the event of a failure. The
caller of these routines can also pass a pointer variable settable by
the routine to track if an error has happened, letting the caller decide
what to do in the event of an error and what error message to generate.
Those routines have been slacking the initialization of the tracking
flag, which can be confusing when reading the code, so add some
safeguards against calls of these parsing routines which could lead to a
dubious result.
The LSN parsing gains an assertion to make sure that the tracking flag
is set, while numeric and float paths initialize the flag to a saner
state.
Author: Jeevan Ladhe
Reviewed-by: Álvaro Herrera, Michael Paquier
Discussion: https://postgr.es/m/CAOgcT0NOM9oR0Hag_3VpyW0uF3iCU=BDUFSPfk9JrWXRcWQHqw@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt/float.c')
-rw-r--r-- | src/backend/utils/adt/float.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 7540ca22efe..77a5d7d42f8 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -337,7 +337,7 @@ float8in(PG_FUNCTION_ARGS) } /* Convenience macro: set *have_error flag (if provided) or throw error */ -#define RETURN_ERROR(throw_error) \ +#define RETURN_ERROR(throw_error, have_error) \ do { \ if (have_error) { \ *have_error = true; \ @@ -376,6 +376,9 @@ float8in_internal_opt_error(char *num, char **endptr_p, double val; char *endptr; + if (have_error) + *have_error = false; + /* skip leading whitespace */ while (*num != '\0' && isspace((unsigned char) *num)) num++; @@ -388,7 +391,8 @@ float8in_internal_opt_error(char *num, char **endptr_p, RETURN_ERROR(ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type %s: \"%s\"", - type_name, orig_string)))); + type_name, orig_string))), + have_error); errno = 0; val = strtod(num, &endptr); @@ -463,9 +467,9 @@ float8in_internal_opt_error(char *num, char **endptr_p, errnumber[endptr - num] = '\0'; RETURN_ERROR(ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("\"%s\" is out of range for " - "type double precision", - errnumber)))); + errmsg("\"%s\" is out of range for type double precision", + errnumber))), + have_error); } } else @@ -473,7 +477,8 @@ float8in_internal_opt_error(char *num, char **endptr_p, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type " "%s: \"%s\"", - type_name, orig_string)))); + type_name, orig_string))), + have_error); } #ifdef HAVE_BUGGY_SOLARIS_STRTOD else @@ -500,7 +505,8 @@ float8in_internal_opt_error(char *num, char **endptr_p, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type " "%s: \"%s\"", - type_name, orig_string)))); + type_name, orig_string))), + have_error); return val; } |