Skip to content

Commit 40b12c6

Browse files
hlinnakaRucha Kulkarni
authored andcommitted
Fix integer overflow in array_agg(), when the array grows too large
If you accumulate many arrays full of NULLs, you could overflow 'nitems', before reaching the MaxAllocSize limit on the allocations. Add an explicit check that the number of items doesn't grow too large. With more than MaxArraySize items, getting the final result with makeArrayResultArr() would fail anyway, so better to error out early. Reported-by: Xint Code Author: Heikki Linnakangas <heikki.linnakangas@iki.fi> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Backpatch-through: 14 Security: CVE-2026-6473 (cherry picked from commit e24fb3247644a9baef72758806d83ec59d914781)
1 parent 0cf2b56 commit 40b12c6

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/backend/utils/adt/arrayfuncs.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5585,6 +5585,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
55855585
ndatabytes;
55865586
char *data;
55875587
int i;
5588+
int newnitems;
55885589

55895590
/*
55905591
* We disallow accumulating null subarrays. Another plausible definition
@@ -5614,6 +5615,14 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
56145615
nitems = ArrayGetNItems(ndims, dims);
56155616
ndatabytes = ARR_SIZE(arg) - ARR_DATA_OFFSET(arg);
56165617

5618+
/* Check that the array doesn't grow too large */
5619+
newnitems = astate->nitems + nitems;
5620+
if (newnitems > MaxArraySize)
5621+
ereport(ERROR,
5622+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5623+
errmsg("array size exceeds the maximum allowed (%zu)",
5624+
MaxArraySize)));
5625+
56175626
if (astate->ndims == 0)
56185627
{
56195628
/* First input; check/save the dimensionality info */
@@ -5679,8 +5688,6 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
56795688
/* Deal with null bitmap if needed */
56805689
if (astate->nullbitmap || ARR_HASNULL(arg))
56815690
{
5682-
int newnitems = astate->nitems + nitems;
5683-
56845691
if (astate->nullbitmap == NULL)
56855692
{
56865693
/*
@@ -5704,7 +5711,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
57045711
nitems);
57055712
}
57065713

5707-
astate->nitems += nitems;
5714+
astate->nitems = newnitems;
57085715
astate->dims[0] += 1;
57095716

57105717
MemoryContextSwitchTo(oldcontext);

0 commit comments

Comments
 (0)