Increase repalloc_array() usage in buffile.c
I was reading some buffile.c code, and I noticed some hand-rolled
versions of repalloc_array() (minus the multiplication check). Since we
already have one usage of repalloc_array() in buffile.c for
BufFile::files, I figured it would probably be alright if we used it in
the other two locations as well.
--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
Attachments:
repalloc_array.difftext/x-patch; charset=utf-8; name=repalloc_array.diffDownload+2-4
Hi,
I was reading some buffile.c code, and I noticed some hand-rolled
versions of repalloc_array() (minus the multiplication check). Since we
already have one usage of repalloc_array() in buffile.c for
BufFile::files, I figured it would probably be alright if we used it in
the other two locations as well.
This seems like a good idea, especially with bfc5cea76d25
which hardens (re-)palloc_array against integer overflow.
I checked and could not find any other occurances in buffile.c that
should be included.
--
Sami Imseih
Amazon Web Services (AWS)
On 29 Jul 2026, at 21:27, Tristan Partin <tristan@partin.io> wrote:
I was reading some buffile.c code, and I noticed some hand-rolled
versions of repalloc_array() (minus the multiplication check). Since we
already have one usage of repalloc_array() in buffile.c for
BufFile::files, I figured it would probably be alright if we used it in
the other two locations as well.
+1 for the concept, but I wonder why these weren't covered by 1b105f9472bdb?
--
Daniel Gustafsson
I was reading some buffile.c code, and I noticed some hand-rolled
versions of repalloc_array() (minus the multiplication check). Since we
already have one usage of repalloc_array() in buffile.c for
BufFile::files, I figured it would probably be alright if we used it in
the other two locations as well.+1 for the concept, but I wonder why these weren't covered by 1b105f9472bdb?
hmm, right. specifically 1b105f9472bdb bulk updated buffile.c. Only explanation
I see is they were missed in a large update.
--
Sami
On Wed, Jul 29, 2026 at 03:21:28PM -0500, Sami Imseih wrote:
hmm, right. specifically 1b105f9472bdb bulk updated buffile.c. Only explanation
I see is they were missed in a large update.
If you are interested in more work in this area, I think that we still
have a couple of holes in contrib/, and even more in the backend code.
Perhaps a LLM scan would help.
The reason why these holes exist is that the patch series of
1b105f9472bdb was quite large, so I have been focusing mainly on
making sure that all the suggested changes were right instead of
trying to fix all of them. The long term picture is that we should do
the switch to do more type enforcements, and that this work is
incremental.
Saying all that, the proposed patch makes sense, and this is
incremental work, so I am going to apply it.
Thanks for caring. :)
--
Michael
On Fri, Jul 31, 2026 at 08:42:21AM +0900, Michael Paquier wrote:
The reason why these holes exist is that the patch series of
1b105f9472bdb was quite large, so I have been focusing mainly on
making sure that all the suggested changes were right instead of
trying to fix all of them. The long term picture is that we should do
the switch to do more type enforcements, and that this work is
incremental.Saying all that, the proposed patch makes sense, and this is
incremental work, so I am going to apply it.
By the way, just mentioning two of these holes, using the following
command in src/backend/storage/file/:
$ git grep -A 2 "alloc(" | grep sizeof
fd.c: fd = (int *) palloc(size * sizeof(int));
fd.c: fd = (int *) repalloc(fd, size * sizeof(int));
Switched these extra two spots, while on it, and applied the result.
--
Michael
On Thu Jul 30, 2026 at 6:42 PM CDT, Michael Paquier wrote:
On Wed, Jul 29, 2026 at 03:21:28PM -0500, Sami Imseih wrote:
hmm, right. specifically 1b105f9472bdb bulk updated buffile.c. Only explanation
I see is they were missed in a large update.If you are interested in more work in this area, I think that we still
have a couple of holes in contrib/, and even more in the backend code.
Perhaps a LLM scan would help.The reason why these holes exist is that the patch series of
1b105f9472bdb was quite large, so I have been focusing mainly on
making sure that all the suggested changes were right instead of
trying to fix all of them. The long term picture is that we should do
the switch to do more type enforcements, and that this work is
incremental.Saying all that, the proposed patch makes sense, and this is
incremental work, so I am going to apply it.Thanks for caring. :)
Let me see what I can come up with. As a committer, how would you like
the patches? I could do file by file, subsystem by subsystem, or just
one large single patch? I don't mind putting in the work to translate
all realloc calls where it makes sense, but I would like to make sure
I organize it in such a way to minimize work for you.
--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
On Fri, Jul 31, 2026 at 03:54:33AM +0000, Tristan Partin wrote:
Let me see what I can come up with. As a committer, how would you like
the patches? I could do file by file, subsystem by subsystem, or just
one large single patch? I don't mind putting in the work to translate
all realloc calls where it makes sense, but I would like to make sure
I organize it in such a way to minimize work for you.
All of these are mechanical changes, and it is possible to do a set of
`git add` to split these across sub-directories. So a single huge
patch does not change much compared to many small ones from my
perspective. So your call.
--
Michael
On Fri Jul 31, 2026 at 2:35 AM CDT, Michael Paquier wrote:
On Fri, Jul 31, 2026 at 03:54:33AM +0000, Tristan Partin wrote:
Let me see what I can come up with. As a committer, how would you like
the patches? I could do file by file, subsystem by subsystem, or just
one large single patch? I don't mind putting in the work to translate
all realloc calls where it makes sense, but I would like to make sure
I organize it in such a way to minimize work for you.All of these are mechanical changes, and it is possible to do a set of
`git add` to split these across sub-directories. So a single huge
patch does not change much compared to many small ones from my
perspective. So your call.
I started working on this yesterday. One pattern that became pretty
noticeable was:
buf = MemoryContextAlloc(context, elements * sizeof(*buf));
...
buf = repalloc(buf, new_elements * sizeof(*buf));
I wonder if we should add MemoreContextAllocArray() and friends, similar
to palloc_array().
--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
On Fri Jul 31, 2026 at 7:35 AM UTC, Michael Paquier wrote:
On Fri, Jul 31, 2026 at 03:54:33AM +0000, Tristan Partin wrote:
Let me see what I can come up with. As a committer, how would you like
the patches? I could do file by file, subsystem by subsystem, or just
one large single patch? I don't mind putting in the work to translate
all realloc calls where it makes sense, but I would like to make sure
I organize it in such a way to minimize work for you.All of these are mechanical changes, and it is possible to do a set of
`git add` to split these across sub-directories. So a single huge
patch does not change much compared to many small ones from my
perspective. So your call.
Here are 3 patches that hope to modernize most vanilla palloc() calls
where appropriate.
--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)