[patch]overallocate memory for curly braces in array_out

Started by Keiichi Hirobealmost 8 years ago5 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t39397
psql -h localhost -U postgres

Built from patchset v3 (message #3), August 18, 2026 at 04:48 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t39397_3 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t39397_3 && git checkout t39397_3

Patchset v3 (message #3) is on t39397_3

Jump to latest
#1Keiichi Hirobe
chalenge.akane@gmail.com

Hi,

Attached is a patch that fixes a bug
for miscounting total number of curly braces in output string in array_out.

Example1
{{1,2,3},{4,5,6}} -> dims[0] = 2, dims[1]= 3

- Without the patch
8
- After
3

Example2
N size one-dimensional array -> dims[0] = N

- Without the patch
N
- After
1

I used gdb to confirm the above behavior.

Cheers,
Keiichi Hirobe

Attachments:

array_out_bugfix.patchapplication/octet-stream; name=array_out_bugfix.patchDownload+1-1
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Keiichi Hirobe (#1)
Re: [patch]overallocate memory for curly braces in array_out

Keiichi Hirobe <chalenge.akane@gmail.com> writes:

Attached is a patch that fixes a bug
for miscounting total number of curly braces in output string in array_out.

Wow, good catch!

Testing this, I found there's a second way in which the space calculation
is off: it always allocated one more byte than required, as a result of
counting one more comma than is really required. That's not nearly as
significant as the curly-brace miscount, but it still got in the way of
doing this:

*** 1234,1239 ****
--- 1243,1251 ----
  #undef APPENDSTR
  #undef APPENDCHAR
+ 	/* Assert that we calculated the string length accurately */
+ 	Assert(overall_length == (p - retval + 1));
+ 
  	pfree(values);
  	pfree(needquotes);

which seemed to me like a good idea now that we know this code isn't
so perfect as all that.

Will push shortly.

regards, tom lane

#3Keiichi Hirobe
chalenge.akane@gmail.com
In reply to: Tom Lane (#2)
Re: [patch]overallocate memory for curly braces in array_out

Hi,

Thanks for the reply.

I am not sure whether to fix another bug, but I fixed and I attached a new
patch,
please check it.

Please note that I inserted a line for updating "overall_length" at line
1185.

I checked below case.

select ARRAY[ARRAY[1,2,3],ARRAY[4,5,6]]; (regular case)
select '[3:4][2:4]={{1,2,3},{4,5,6}}'::_int4; (needdims case)
select ARRAY['a"bc','def']; (needquote case)

Cheers,
Keiichi Hirobe

2018年9月24日(月) 23:46 Tom Lane <tgl@sss.pgh.pa.us>:

Show quoted text

Keiichi Hirobe <chalenge.akane@gmail.com> writes:

Attached is a patch that fixes a bug
for miscounting total number of curly braces in output string in

array_out.

Wow, good catch!

Testing this, I found there's a second way in which the space calculation
is off: it always allocated one more byte than required, as a result of
counting one more comma than is really required. That's not nearly as
significant as the curly-brace miscount, but it still got in the way of
doing this:

*** 1234,1239 ****
--- 1243,1251 ----
#undef APPENDSTR
#undef APPENDCHAR
+       /* Assert that we calculated the string length accurately */
+       Assert(overall_length == (p - retval + 1));
+
pfree(values);
pfree(needquotes);

which seemed to me like a good idea now that we know this code isn't
so perfect as all that.

Will push shortly.

regards, tom lane

Attachments:

t39397_3
array_out_bugfix2.patchapplication/octet-stream; name=array_out_bugfix2.patchDownload+9-3
#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Keiichi Hirobe (#3)
Re: [patch]overallocate memory for curly braces in array_out

Keiichi Hirobe <chalenge.akane@gmail.com> writes:

I am not sure whether to fix another bug, but I fixed and I attached a new
patch,
please check it.

I think this duplicates what I already committed at

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=87d9bbca13f9c6b8f6ee986f0e399cb83bd731d4

regards, tom lane

#5Keiichi Hirobe
chalenge.akane@gmail.com
In reply to: Tom Lane (#4)
Re: [patch]overallocate memory for curly braces in array_out

Sorry, I did not notice that you had already pushed it.
Thank you for quick action.

Cheers,
Keiichi Hirobe

2018年9月28日(金) 9:23 Tom Lane <tgl@sss.pgh.pa.us>:

Show quoted text

Keiichi Hirobe <chalenge.akane@gmail.com> writes:

I am not sure whether to fix another bug, but I fixed and I attached a

new

patch,
please check it.

I think this duplicates what I already committed at

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=87d9bbca13f9c6b8f6ee986f0e399cb83bd731d4

regards, tom lane