This is an automated email from the ASF dual-hosted git repository.

tuhaihe pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit ef75653ef0c0c350cba7af73564bcddafde23921
Author: Heikki Linnakangas <[email protected]>
AuthorDate: Mon May 11 05:13:50 2026 -0700

    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 <[email protected]>
    Reviewed-by: Tom Lane <[email protected]>
    Backpatch-through: 14
    Security: CVE-2026-6473
---
 src/backend/utils/adt/arrayfuncs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/adt/arrayfuncs.c 
b/src/backend/utils/adt/arrayfuncs.c
index 3f4c267c00a..ff3e1af0a1d 100644
--- a/src/backend/utils/adt/arrayfuncs.c
+++ b/src/backend/utils/adt/arrayfuncs.c
@@ -5631,6 +5631,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
                                ndatabytes;
        char       *data;
        int                     i;
+       int                     newnitems;
 
        /*
         * We disallow accumulating null subarrays.  Another plausible 
definition
@@ -5660,6 +5661,14 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
        nitems = ArrayGetNItems(ndims, dims);
        ndatabytes = ARR_SIZE(arg) - ARR_DATA_OFFSET(arg);
 
+       /* Check that the array doesn't grow too large */
+       newnitems = astate->nitems + nitems;
+       if (newnitems > MaxArraySize)
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("array size exceeds the maximum allowed 
(%zu)",
+                                               MaxArraySize)));
+
        if (astate->ndims == 0)
        {
                /* First input; check/save the dimensionality info */
@@ -5725,8 +5734,6 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
        /* Deal with null bitmap if needed */
        if (astate->nullbitmap || ARR_HASNULL(arg))
        {
-               int                     newnitems = astate->nitems + nitems;
-
                if (astate->nullbitmap == NULL)
                {
                        /*
@@ -5750,7 +5757,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
                                                  nitems);
        }
 
-       astate->nitems += nitems;
+       astate->nitems = newnitems;
        astate->dims[0] += 1;
 
        MemoryContextSwitchTo(oldcontext);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to