pgsql: Fix over-allocation of space for array_out()'s result string.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix over-allocation of space for array_out()'s result string.
Date: 2018-09-24 15:31:29
Message-ID: E1g4Spd-0004qE-EZ@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix over-allocation of space for array_out()'s result string.

array_out overestimated the space needed for its output, possibly by
a very substantial amount if the array is multi-dimensional, because
of wrong order of operations in the loop that counts the number of
curly-brace pairs needed. While the output string is normally
short-lived, this could still cause problems in extreme cases.

An additional minor error was that it counted one more delimiter than
is actually needed.

Repair those errors, add an Assert that the space is now correctly
calculated, and make some minor improvements in the comments.

I also failed to resist the temptation to get rid of an integer
modulus operation per array element; a simple comparison is sufficient.

This bug dates clear back to Berkeley days, so back-patch to all
supported versions.

Keiichi Hirobe, minor additional work by me

Discussion: https://postgr.es/m/CAH=EFxE9W0tRvQkixR2XJRRCToUYUEDkJZk6tnADXugPBRdcdg@mail.gmail.com

Branch
------
REL9_6_STABLE

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

Modified Files
--------------
src/backend/utils/adt/arrayfuncs.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Nasby, Jim 2018-09-24 18:25:46 Re: pgsql: Improve autovacuum logging for aggressive and anti-wraparound ru
Previous Message Joe Conway 2018-09-24 14:15:32 pgsql: Document aclitem functions and operators