]> granicus.if.org Git - postgresql/commit
Fix over-allocation of space for array_out()'s result string.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 24 Sep 2018 15:30:51 +0000 (11:30 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 24 Sep 2018 15:30:51 +0000 (11:30 -0400)
commit7ecdeb5f5476988af9f21dc63500bdb3fa39aaad
tree0f4a4979b2c9b73164b5b2f58c43e3f912147ad8
parent402da7054f34d2c5e3215ca72fc3d08ca2c98090
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
src/backend/utils/adt/arrayfuncs.c