diff options
author | Nathan Bossart <nathan@postgresql.org> | 2024-07-23 21:59:02 -0500 |
---|---|---|
committer | Nathan Bossart <nathan@postgresql.org> | 2024-07-23 21:59:02 -0500 |
commit | 878e8c6be7ab2523b3dcbe238578e95162f521e9 (patch) | |
tree | f0a7294331f3d9d6bcd5ba60023a9a5ca3c08eb7 | |
parent | 77b116724c7545fb610e0f7287d3fbff35ffea22 (diff) | |
download | postgresql-878e8c6be7ab2523b3dcbe238578e95162f521e9.tar.gz postgresql-878e8c6be7ab2523b3dcbe238578e95162f521e9.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
-rw-r--r-- | src/backend/utils/adt/arrayfuncs.c | 9 | ||||
-rw-r--r-- | src/test/regress/expected/arrays.out | 4 | ||||
-rw-r--r-- | src/test/regress/sql/arrays.sql | 2 |
3 files changed, 14 insertions, 1 deletions
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 49817303a9b..37916774da6 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -2871,7 +2871,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]; } diff --git a/src/test/regress/expected/arrays.out b/src/test/regress/expected/arrays.out index e4ec394bc40..a09f21580f2 100644 --- a/src/test/regress/expected/arrays.out +++ b/src/test/regress/expected/arrays.out @@ -1356,6 +1356,10 @@ update arr_pk_tbl set f1[2147483647] = 42 where pk = 10; ERROR: 54000 update arr_pk_tbl set f1[2147483646:2147483647] = array[4,2] where pk = 10; ERROR: 54000 +insert into arr_pk_tbl(pk, f1[0:2147483647]) values (2, '{}'); +ERROR: 54000 +insert into arr_pk_tbl(pk, f1[-2147483648:2147483647]) values (2, '{}'); +ERROR: 54000 -- also exercise the expanded-array case do $$ declare a int[]; begin diff --git a/src/test/regress/sql/arrays.sql b/src/test/regress/sql/arrays.sql index 4ad6e556c29..069f8016fb6 100644 --- a/src/test/regress/sql/arrays.sql +++ b/src/test/regress/sql/arrays.sql @@ -416,6 +416,8 @@ reset enable_bitmapscan; insert into arr_pk_tbl values(10, '[-2147483648:-2147483647]={1,2}'); update arr_pk_tbl set f1[2147483647] = 42 where pk = 10; update arr_pk_tbl set f1[2147483646:2147483647] = array[4,2] where pk = 10; +insert into arr_pk_tbl(pk, f1[0:2147483647]) values (2, '{}'); +insert into arr_pk_tbl(pk, f1[-2147483648:2147483647]) values (2, '{}'); -- also exercise the expanded-array case do $$ declare a int[]; |