diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c index 6b63f93e6de20..bdf59a10fcfec 100644 --- a/src/backend/executor/execExprInterp.c +++ b/src/backend/executor/execExprInterp.c @@ -2706,7 +2706,7 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op) { /* Must be nested array expressions */ int nbytes = 0; - int nitems = 0; + int nitems; int outer_nelems = 0; int elem_ndims = 0; int *elem_dims = NULL; @@ -2801,9 +2801,14 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op) subbitmaps[outer_nelems] = ARR_NULLBITMAP(array); subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array); nbytes += subbytes[outer_nelems]; + /* check for overflow of total request */ + if (!AllocSizeIsValid(nbytes)) + ereport(ERROR, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("array size exceeds the maximum allowed (%d)", + (int) MaxAllocSize))); subnitems[outer_nelems] = ArrayGetNItems(this_ndims, ARR_DIMS(array)); - nitems += subnitems[outer_nelems]; havenulls |= ARR_HASNULL(array); outer_nelems++; } @@ -2837,7 +2842,7 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op) } /* check for subscript overflow */ - (void) ArrayGetNItems(ndims, dims); + nitems = ArrayGetNItems(ndims, dims); ArrayCheckBounds(ndims, dims, lbs); if (havenulls) @@ -2851,7 +2856,7 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op) nbytes += ARR_OVERHEAD_NONULLS(ndims); } - result = (ArrayType *) palloc(nbytes); + result = (ArrayType *) palloc0(nbytes); SET_VARSIZE(result, nbytes); result->ndim = ndims; result->dataoffset = dataoffset; diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 55ba9f73deb99..949737d89f701 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -2459,8 +2459,7 @@ array_set_element(Datum arraydatum, { bits8 *newnullbitmap = ARR_NULLBITMAP(newarray); - /* Zero the bitmap to take care of marking inserted positions null */ - MemSet(newnullbitmap, 0, (newnitems + 7) / 8); + /* palloc0 above already marked any inserted positions as nulls */ /* Fix the inserted value */ if (addedafter) array_set_isnull(newnullbitmap, newnitems - 1, isNull); @@ -3076,8 +3075,7 @@ array_set_slice(Datum arraydatum, bits8 *newnullbitmap = ARR_NULLBITMAP(newarray); bits8 *oldnullbitmap = ARR_NULLBITMAP(array); - /* Zero the bitmap to handle marking inserted positions null */ - MemSet(newnullbitmap, 0, (nitems + 7) / 8); + /* palloc0 above already marked any inserted positions as nulls */ array_bitmap_copy(newnullbitmap, addedbefore, oldnullbitmap, 0, itemsbefore);