From d5dc90c8854d949e186c0c3d05d4c528737f169e Mon Sep 17 00:00:00 2001 From: pgaddict Date: Sat, 2 Sep 2023 18:11:52 +0800 Subject: [PATCH v4 7/7] refactor ReadDimensionInt. Previouly(v3) when parse the string and convert to a value range outside of int via strtol. then assigned to a int variable, which is undefined behavior, but it won't generate error. So now, parse and make the result integer be the range of PG_INT32_MIN, PG_INT32_MAX (inclusive). --- src/backend/utils/adt/arrayfuncs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index cce6b564..3e000f20 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -477,8 +477,8 @@ ReadArrayDimensions(char **srcptr, int *ndim_p, int *dim, int *lBound, "]"))); p++; - /* Upper bound of INT_MAX is disallowed, cf ArrayCheckBounds() */ - if (ub == INT_MAX) + /* Upper bound of PG_INT32_MAX is disallowed, cf ArrayCheckBounds() */ + if (ub == PG_INT32_MAX) ereturn(escontext, false, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("array upper bound is too large: %d", ub))); @@ -513,6 +513,7 @@ static bool ReadDimensionInt(char **srcptr, int *result, const char *origStr, Node *escontext) { char *p = *srcptr; + long l; /* don't accept leading whitespace */ if (!isdigit((unsigned char) *p) && *p != '-' && *p != '+') @@ -522,11 +523,16 @@ ReadDimensionInt(char **srcptr, int *result, const char *origStr, Node *escontex } errno = 0; - *result = strtol(p, srcptr, 10); - if (errno == ERANGE) + l = strtol(p, srcptr, 10); + + if (errno == ERANGE || l > PG_INT32_MAX || l < PG_INT32_MIN) + { ereturn(escontext, false, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("array bound is out of range"))); + } + + *result = l; return true; } @@ -595,12 +601,6 @@ ReadArrayStr(char **srcptr, /* The caller already checked this */ Assert(**srcptr == '{'); - if (!dimensions_specified) - { - /* Initialize dim[] entries to -1 meaning "unknown" */ - for (int i = 0; i < MAXDIM; ++i) - dim[i] = -1; - } ndim_frozen = dimensions_specified; maxitems = 16; -- 2.34.1