aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/arrayfuncs.c
diff options
context:
space:
mode:
authorNathan Bossart <nathan@postgresql.org>2024-07-23 21:59:02 -0500
committerNathan Bossart <nathan@postgresql.org>2024-07-23 21:59:02 -0500
commita57d168653900903e8b1b4fe91d62de2c2f2693d (patch)
tree6603b1af1ffd8d91e8d20ddf4522330c8e5377b4 /src/backend/utils/adt/arrayfuncs.c
parent74e15462848de9cc9c302a8f6e12c3b04288b6a1 (diff)
downloadpostgresql-a57d168653900903e8b1b4fe91d62de2c2f2693d.tar.gz
postgresql-a57d168653900903e8b1b4fe91d62de2c2f2693d.zip
Detect integer overflow in array_set_slice().
When provided an empty initial array, array_set_slice() fails to check for overflow when computing the new array's dimensions. While such overflows are ordinarily caught by ArrayGetNItems(), commands with the following form are accepted: INSERT INTO t (i[-2147483648:2147483647]) VALUES ('{}'); To fix, perform the hazardous computations using overflow-detecting arithmetic routines. As with commit 18b585155a, the added test cases generate errors that include a platform-dependent value, so we again use psql's VERBOSITY parameter to suppress printing the message text. Reported-by: Alexander Lakhin Author: Joseph Koshakow Reviewed-by: Jian He Discussion: https://postgr.es/m/31ad2cd1-db94-bdb3-f91a-65ffdb4bef95%40gmail.com Backpatch-through: 12
Diffstat (limited to 'src/backend/utils/adt/arrayfuncs.c')
-rw-r--r--src/backend/utils/adt/arrayfuncs.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c
index c5336940dab..57c12df0f73 100644
--- a/src/backend/utils/adt/arrayfuncs.c
+++ b/src/backend/utils/adt/arrayfuncs.c
@@ -2923,7 +2923,14 @@ array_set_slice(Datum arraydatum,
errdetail("When assigning to a slice of an empty array value,"
" slice boundaries must be fully specified.")));
- dim[i] = 1 + upperIndx[i] - lowerIndx[i];
+ /* compute "upperIndx[i] - lowerIndx[i] + 1", detecting overflow */
+ if (pg_sub_s32_overflow(upperIndx[i], lowerIndx[i], &dim[i]) ||
+ pg_add_s32_overflow(dim[i], 1, &dim[i]))
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("array size exceeds the maximum allowed (%d)",
+ (int) MaxArraySize)));
+
lb[i] = lowerIndx[i];
}