Skip to content

Commit e947458

Browse files
hlinnakaanju15bharti
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 67dd6243dc95df560ff3c31ed5b6e9474d98c4c3)
1 parent 073e734 commit e947458

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
@@ -5567,6 +5567,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
55675567
ndatabytes;
55685568
char *data;
55695569
int i;
5570+
int newnitems;
55705571

55715572
/*
55725573
* We disallow accumulating null subarrays. Another plausible definition
@@ -5596,6 +5597,14 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
55965597
nitems = ArrayGetNItems(ndims, dims);
55975598
ndatabytes = ARR_SIZE(arg) - ARR_DATA_OFFSET(arg);
55985599

5600+
/* Check that the array doesn't grow too large */
5601+
newnitems = astate->nitems + nitems;
5602+
if (newnitems > MaxArraySize)
5603+
ereport(ERROR,
5604+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5605+
errmsg("array size exceeds the maximum allowed (%zu)",
5606+
MaxArraySize)));
5607+
55995608
if (astate->ndims == 0)
56005609
{
56015610
/* First input; check/save the dimensionality info */
@@ -5661,8 +5670,6 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
56615670
/* Deal with null bitmap if needed */
56625671
if (astate->nullbitmap || ARR_HASNULL(arg))
56635672
{
5664-
int newnitems = astate->nitems + nitems;
5665-
56665673
if (astate->nullbitmap == NULL)
56675674
{
56685675
/*
@@ -5686,7 +5693,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
56865693
nitems);
56875694
}
56885695

5689-
astate->nitems += nitems;
5696+
astate->nitems = newnitems;
56905697
astate->dims[0] += 1;
56915698

56925699
MemoryContextSwitchTo(oldcontext);

0 commit comments

Comments
 (0)