pgsql: Fix oversights in array manipulation. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix oversights in array manipulation.
Date
Msg-id E1pgUMl-005Vxj-Pv@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix oversights in array manipulation.

The nested-arrays code path in ExecEvalArrayExpr() used palloc to
allocate the result array, whereas every other array-creating function
has used palloc0 since 18c0b4ecc.  This mostly works, but unused bits
past the end of the nulls bitmap may end up undefined.  That causes
valgrind complaints with -DWRITE_READ_PARSE_PLAN_TREES, and could
cause planner misbehavior as cited in 18c0b4ecc.  There seems no very
good reason why we should strive to avoid palloc0 in just this one case,
so fix it the easy way with s/palloc/palloc0/.

While looking at that I noted that we also failed to check for overflow
of "nbytes" and "nitems" while summing the sizes of the sub-arrays,
potentially allowing a crash due to undersized output allocation.
For "nbytes", follow the policy used by other array-munging code of
checking for overflow after each addition.  (As elsewhere, the last
addition of the array's overhead space doesn't need an extra check,
since palloc itself will catch a value between 1Gb and 2Gb.)
For "nitems", there's no very good reason to sum the inputs at all,
since we can perfectly well use ArrayGetNItems' result instead of
ignoring it.

Per discussion of this bug, also remove redundant zeroing of the
nulls bitmap in array_set_element and array_set_slice.

Patch by Alexander Lakhin and myself, per bug #17858 from Alexander
Lakhin; thanks also to Richard Guo.  These bugs are a dozen years old,
so back-patch to all supported branches.

Discussion: https://postgr.es/m/17858-8fd287fd3663d051@postgresql.org

Branch
------
REL_12_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/ad5fe7420594811d3ca4054a8397c5cc8c6575e2

Modified Files
--------------
src/backend/executor/execExprInterp.c | 13 +++++++++----
src/backend/utils/adt/arrayfuncs.c    |  6 ++----
2 files changed, 11 insertions(+), 8 deletions(-)


pgsql-committers by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: pgsql: Fix netmask handling in inet_minmax_multi_ops
Next
From: Tom Lane
Date:
Subject: Re: pgsql: Fix netmask handling in inet_minmax_multi_ops