diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-27 11:40:01 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-27 11:40:01 -0500 |
commit | eb8312a22a84c33fc405ae9b497113973f552f90 (patch) | |
tree | 3d7a135bafd3fd06750a6586e69225447743caae /src/backend/utils/adt/oid.c | |
parent | 63c844a0a5d70cdbd6ae0470d582d39e75ad8d66 (diff) | |
download | postgresql-eb8312a22a84c33fc405ae9b497113973f552f90.tar.gz postgresql-eb8312a22a84c33fc405ae9b497113973f552f90.zip |
Detect bad input for types xid, xid8, and cid.
Historically these input functions just called strtoul or strtoull
and returned the result, with no error detection whatever. Upgrade
them to reject garbage input and out-of-range values, similarly to
our other numeric input routines.
To share the code for this with type oid, adjust the existing
"oidin_subr" to be agnostic about the SQL name of the type it is
handling, and move it to numutils.c; then clone it for 64-bit types.
Because the xid types previously accepted hex and octal input by
reason of calling strtoul[l] with third argument zero, I made the
common subroutine do that too, with the consequence that type oid
now also accepts hex and octal input. In view of 6fcda9aba, that
seems like a good thing.
While at it, simplify the existing over-complicated handling of
syntax errors from strtoul: we only need one ereturn not three.
Discussion: https://postgr.es/m/3526121.1672000729@sss.pgh.pa.us
Diffstat (limited to 'src/backend/utils/adt/oid.c')
-rw-r--r-- | src/backend/utils/adt/oid.c | 101 |
1 files changed, 5 insertions, 96 deletions
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c index 9d382b5cb7c..6b70b774d58 100644 --- a/src/backend/utils/adt/oid.c +++ b/src/backend/utils/adt/oid.c @@ -32,106 +32,13 @@ * USER I/O ROUTINES * *****************************************************************************/ -/* - * Parse a single OID and return its value. - * - * If endloc isn't NULL, store a pointer to the rest of the string there, - * so that caller can parse the rest. Otherwise, it's an error if anything - * but whitespace follows. - * - * If escontext points to an ErrorSaveContext node, that is filled instead - * of throwing an error; the caller must check SOFT_ERROR_OCCURRED() - * to detect errors. - */ -static Oid -oidin_subr(const char *s, char **endloc, Node *escontext) -{ - unsigned long cvt; - char *endptr; - Oid result; - - if (*s == '\0') - ereturn(escontext, InvalidOid, - (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("invalid input syntax for type %s: \"%s\"", - "oid", s))); - - errno = 0; - cvt = strtoul(s, &endptr, 10); - - /* - * strtoul() 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) - ereturn(escontext, InvalidOid, - (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("invalid input syntax for type %s: \"%s\"", - "oid", s))); - - if (endptr == s && *s != '\0') - ereturn(escontext, InvalidOid, - (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("invalid input syntax for type %s: \"%s\"", - "oid", s))); - - if (errno == ERANGE) - ereturn(escontext, InvalidOid, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("value \"%s\" is out of range for type %s", - s, "oid"))); - - if (endloc) - { - /* caller wants to deal with rest of string */ - *endloc = endptr; - } - else - { - /* allow only whitespace after number */ - while (*endptr && isspace((unsigned char) *endptr)) - endptr++; - if (*endptr) - ereturn(escontext, InvalidOid, - (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("invalid input syntax for type %s: \"%s\"", - "oid", s))); - } - - result = (Oid) cvt; - - /* - * Cope with possibility that unsigned long is wider than Oid, in which - * case strtoul will not raise an error for some values that are out of - * the range of Oid. - * - * For backwards compatibility, we want to accept inputs that are given - * with a minus sign, so allow the input value if it matches after either - * signed or unsigned extension to long. - * - * To ensure consistent results on 32-bit and 64-bit platforms, make sure - * the error message is the same as if strtoul() had returned ERANGE. - */ -#if OID_MAX != ULONG_MAX - if (cvt != (unsigned long) result && - cvt != (unsigned long) ((int) result)) - ereturn(escontext, InvalidOid, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("value \"%s\" is out of range for type %s", - s, "oid"))); -#endif - - return result; -} - Datum oidin(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); Oid result; - result = oidin_subr(s, NULL, fcinfo->context); + result = uint32in_subr(s, NULL, "oid", fcinfo->context); PG_RETURN_OID(result); } @@ -218,7 +125,8 @@ oidvectorin(PG_FUNCTION_ARGS) oidString++; if (*oidString == '\0') break; - result->values[n] = oidin_subr(oidString, &oidString, escontext); + result->values[n] = uint32in_subr(oidString, &oidString, + "oid", escontext); if (SOFT_ERROR_OCCURRED(escontext)) PG_RETURN_NULL(); } @@ -339,7 +247,8 @@ oidparse(Node *node) * constants by the lexer. Accept these if they are valid OID * strings. */ - return oidin_subr(castNode(Float, node)->fval, NULL, NULL); + return uint32in_subr(castNode(Float, node)->fval, NULL, + "oid", NULL); default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); } |