use the malloc macros in pg_dump.c

Started by Peter Smithabout 1 month ago7 messages
Jump to latest
#1Peter Smith
smithpb2250@gmail.com

Hi.

I found that pg_dump.c has many pg_mallocs where the new object/array
macros could have been used, but currently they are not. I'm not sure
if this was a deliberate or accidental omission.

In case it was accidental, here is a patch to modify/simplify all that
allocation code.

PSA v1.

======
Kind Regards,
Peter Smith.
Fujitsu Australia

Attachments:

v1-0001-use-the-malloc-macros-for-arrays-and-objects.patchapplication/octet-stream; name=v1-0001-use-the-malloc-macros-for-arrays-and-objects.patchDownload+82-85
#2Aleksander Alekseev
aleksander@timescale.com
In reply to: Peter Smith (#1)
Re: use the malloc macros in pg_dump.c

Hi Peter,

I found that pg_dump.c has many pg_mallocs where the new object/array
macros could have been used, but currently they are not. I'm not sure
if this was a deliberate or accidental omission.

In case it was accidental, here is a patch to modify/simplify all that
allocation code.

Thanks for the patch. I reviewed / tested it and it looks OK. This
being said, I see many pieces of code left that still use pg_malloc /
pg_realloc in a similar fashion, including src/bin/pg_dump/* files.
Would you like to address those as well, or keep the scope only to
pg_dump.c?

--
Best regards,
Aleksander Alekseev

#3Peter Smith
smithpb2250@gmail.com
In reply to: Aleksander Alekseev (#2)
Re: use the malloc macros in pg_dump.c

On Mon, Feb 2, 2026 at 11:38 PM Aleksander Alekseev
<aleksander@tigerdata.com> wrote:

Hi Peter,

I found that pg_dump.c has many pg_mallocs where the new object/array
macros could have been used, but currently they are not. I'm not sure
if this was a deliberate or accidental omission.

In case it was accidental, here is a patch to modify/simplify all that
allocation code.

Thanks for the patch. I reviewed / tested it and it looks OK. This
being said, I see many pieces of code left that still use pg_malloc /
pg_realloc in a similar fashion, including src/bin/pg_dump/* files.
Would you like to address those as well, or keep the scope only to
pg_dump.c?

Hi Aleksander.

Thanks for your review!

At first I only changed only one file because I was unsure why these
changes were not already made, and whether others thought it was
worthwhile.

Now you have encouraged me that the patch makes sense, so I will
increase the scope to cover all the other pg_dump/* files. Will post
more later.

======
Kind Regards,
Peter Smith.
Fujitsu Australia

#4Peter Smith
smithpb2250@gmail.com
In reply to: Peter Smith (#3)
Re: use the malloc macros in pg_dump.c

PSA v2 with more of the same kind of malloc changes.

Scope: This addresses all those I could find in the src/bin/pg_dump/* folder.

Note, there were a few examples of simple char * buffer mallocs I that
did not change, because it did not seem useful to do so.

======
Kind Regards,
Peter Smith.
Fujitsu Australia

Attachments:

v2-0007-modify-malloc-for-compress_none.c.patchapplication/octet-stream; name=v2-0007-modify-malloc-for-compress_none.c.patchDownload+1-2
v2-0005-modify-malloc-for-pg_backup_directory.c.patchapplication/octet-stream; name=v2-0005-modify-malloc-for-pg_backup_directory.c.patchDownload+4-5
v2-0004-modify-malloc-for-pg_backup_archiver.c.patchapplication/octet-stream; name=v2-0004-modify-malloc-for-pg_backup_archiver.c.patchDownload+12-13
v2-0001-use-the-malloc-macros-for-arrays-and-objects.patchapplication/octet-stream; name=v2-0001-use-the-malloc-macros-for-arrays-and-objects.patchDownload+82-85
v2-0003-modify-malloc-in-pg_dump_sort.c.patchapplication/octet-stream; name=v2-0003-modify-malloc-in-pg_dump_sort.c.patchDownload+6-7
v2-0002-modify-mallocs-in-dumputils.c.patchapplication/octet-stream; name=v2-0002-modify-mallocs-in-dumputils.c.patchDownload+4-5
v2-0008-modify-malloc-for-compress_gzip.c.patchapplication/octet-stream; name=v2-0008-modify-malloc-for-compress_gzip.c.patchDownload+3-4
v2-0006-modify-malloc-for-parallel.c.patchapplication/octet-stream; name=v2-0006-modify-malloc-for-parallel.c.patchDownload+7-8
v2-0009-modify-malloc-for-pg_backup_custom.c.patchapplication/octet-stream; name=v2-0009-modify-malloc-for-pg_backup_custom.c.patchDownload+4-5
v2-0010-modify-malloc-for-compress_io.c.patchapplication/octet-stream; name=v2-0010-modify-malloc-for-compress_io.c.patchDownload+2-3
v2-0011-modify-malloc-for-pg_dumpall.c.patchapplication/octet-stream; name=v2-0011-modify-malloc-for-pg_dumpall.c.patchDownload+1-2
v2-0012-modify-more-mallocs-in-pg_dump.c.patchapplication/octet-stream; name=v2-0012-modify-more-mallocs-in-pg_dump.c.patchDownload+5-6
v2-0013-modify-malloc-for-pg_backup_archiver.c.patchapplication/octet-stream; name=v2-0013-modify-malloc-for-pg_backup_archiver.c.patchDownload+8-9
v2-0015-modify-malloc-for-compress_zstd.c.patchapplication/octet-stream; name=v2-0015-modify-malloc-for-compress_zstd.c.patchDownload+1-2
v2-0014-modify-malloc-for-compress_lz4.c.patchapplication/octet-stream; name=v2-0014-modify-malloc-for-compress_lz4.c.patchDownload+2-3
v2-0016-modify-malloc-for-connectdb.c.patchapplication/octet-stream; name=v2-0016-modify-malloc-for-connectdb.c.patchDownload+4-5
#5Peter Smith
smithpb2250@gmail.com
In reply to: Peter Smith (#4)
Re: use the malloc macros in pg_dump.c

Hi.

PSA v3. A rebase was needed.

At the same time, I combined all previous patches.

======
Kind Regards,
Peter Smith.
Fujitsu Australia

Attachments:

v3-0001-use-the-malloc-macros-for-in-pg_dump-code.patchapplication/octet-stream; name=v3-0001-use-the-malloc-macros-for-in-pg_dump-code.patchDownload+145-148
#6Michael Paquier
michael@paquier.xyz
In reply to: Peter Smith (#5)
Re: use the malloc macros in pg_dump.c

On Fri, Feb 13, 2026 at 06:06:26PM +1100, Peter Smith wrote:

At the same time, I combined all previous patches.

That's a practice better than sending a handful bag of patches that
each touch one single file. :)

Seems globally clean to me, except for two changes.

-   zp = gzipcs->zp = (z_streamp) pg_malloc(sizeof(z_stream));
+   zp = gzipcs->zp = (z_streamp) pg_malloc_object(z_stream);
[...]
-   zp = (z_streamp) pg_malloc(sizeof(z_stream));
+   zp = (z_streamp) pg_malloc_object(z_stream);

These two defeat the purpose of the change. _object is useful because
we can enforce type checks based on their size. Note that We have
this declaration in zlib:
zlib.h:typedef z_stream FAR *z_streamp;

So we should be OK with dropping the casts entirely, with I guess
compilers not warning if types do not match as an effect of this
typedef declaration. I have removed these, and things seem OK here,
now to say if the buildfarm is entirely OK will be a different thing.
I'll see about that if/once required.
--
Michael

#7Peter Smith
smithpb2250@gmail.com
In reply to: Michael Paquier (#6)
Re: use the malloc macros in pg_dump.c

Thanks for pushing!

======
Kind Regards,
Peter Smith.
Fujitsu Australia