Re: Use stack-allocated StringInfoData - Mailing list pgsql-hackers

From Mats Kindahl
Subject Re: Use stack-allocated StringInfoData
Date
Msg-id 1865ee50-8290-4062-b062-5f5ab17364f2@gmail.com
Whole thread Raw
In response to Re: Use stack-allocated StringInfoData  (David Rowley <dgrowleyml@gmail.com>)
Responses Re: Use stack-allocated StringInfoData
List pgsql-hackers


On 11/3/25 11:27, David Rowley wrote:
On Mon, 3 Nov 2025 at 20:27, Mats Kindahl <mats.kindahl@gmail.com> wrote:
We can use
       StringInfoData info;       initStringInfo(&info);       ...       appendStringInfo(&info, ...);       ...       return info.data;

It was corrected in an earlier commit, but that seems to have been removed so we still have a lot of these cases.

I created a semantic patch to capture most of these cases, which is present in [1], but this is a slightly modified version that might be interesting to include regardless of other changes. The patch is applied and one case that couldn't be matched is manually fixed.

Hi David, and thanks for the review.

I think this is a worthwhile conversion. Are you able to create a more
complete version of this? The draft version does introduce quite a few
whitespace changes that aren't wanted.

Absolutely, I'll look into it.

You've also introduced a memory
leak in check_publications(), fetch_relation_list(), jsonb_send() and
xml_errorHandler() (NB: destroyStringInfo() doesn't just pfree the
memory for the struct, it pfree's the data too).

Ah, that was a mistake from my side. Will fix.

The patch shouldn't
be leaving any memory around that the current master is careful to
pfree.

Do you have any semi-automated method to find these? Or is it a case
of manually reviewing code with a makeStringInfo() call?

This is using Coccinelle to transform the text based on a semantic patch (which is included in the patch). Unfortunately it mess with the whitespace a bit so it is necessary to run pg_intent after.  I'm surprised that there are whitespace changes that should not be there, but I'll take a look. Thanks again, and best wishes,
Mats Kindahl

pgsql-hackers by date:

Previous
From: Álvaro Herrera
Date:
Subject: Re: psql --help=variables missing csv_fieldsep
Next
From: "Matheus Alcantara"
Date:
Subject: Re: Have the planner convert COUNT(1) / COUNT(not_null_col) to COUNT(*)