Re: Use pg_malloc macros in src/fe_utils

Andreas Karlsson <andreas@proxel.se>

From: Andreas Karlsson <andreas@proxel.se>
To: Michael Paquier <michael@paquier.xyz>
Cc: Henrik TJ <henrik@0x48.dk>, PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2026-02-27T01:15:46Z
Lists: pgsql-hackers

Attachments

On 2/24/26 4:36 AM, Michael Paquier wrote:
> On Mon, Feb 23, 2026 at 03:17:52AM +0100, Andreas Karlsson wrote:
>> Looks like a nice change but why not just fix all instances of it in one
>> swoop? It cannot be that many as there are 166 calls to pg_malloc() and 62
>> calls to pg_malloc0() after your patch that need to be looked at.
> 
> FWIW, I don't really mind if these changes are proposed gradually, and
> this looked fine enough on its own.  So applied.


Fair, here is a patch which should handle all uses in the frontend code 
so we follow this pattern consistently to encourage new code to use 
these macros.

When doing this I found two things which I am ot sure what the cleanest 
way to handle would be so I broke them out into separate patches.

1. What should we do about when we allocate a an array of characters? 
Would it make sense to use pg_array_alloc() or would that jsut be silly? 
For example:

-pad = (char *) pg_malloc(l + 1);
+pad = pg_malloc_array(char, l + 1);

2. I found a small and harmless thinko. The buffer in verify_tar_file() 
is actually a char * but for some reason the code did the following:

buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));

What should we do about it? Just skip the "sizof(uint8)"?

Andreas

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Use pg_malloc_object() and pg_alloc_array() variants in frontend code

  2. fe_utils: Sprinkle some pg_malloc_object() and pg_malloc_array()