Re: Fix mismatched deallocation functions

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: Tristan Partin <tristan(at)partin(dot)io>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix mismatched deallocation functions
Date: 2026-05-07 22:00:54
Message-ID: CAN4CZFMUm-JBTDiWqe67NJ6v21EB=Kua8JWYn3F+eQ7WBhcrEw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello!

There are many cases missed by the script, for example:

tab-complete.in.c:7089:

`previous_words = pg_malloc_array(char *, point);`

tab-complete.in.c:6364:

`completion_ref_object = pg_strdup(word);`

tab-complete.in.c:7090:

`*buffer = (char *) pg_malloc(point * 2);`

There's also completion_ref_schema, which is an out parameter of
parse_identifier, still freed in the patch.

The strtokx change in stringutils.c is also strange - the patch
converts one free at line 96, and leaves the same free a few lines
above at line 73 as is.

> I generated the patch with the help of Coccinelle[0]. I'm no expert with
> Coccinelle, but it seemed like a good candidate to get this refactor
> done. You can run the attached script in your tree with the following
> command:

If I had to do it, I would try to approach this with static analysis
tools instead: a custom rule that enforces attribute declarations for
return values / output parameters allocated by pg_malloc and similar
functions. Without attributes everywhere, these checks will never be
complete because tools won't be able to fully reason about cross
source file call paths.
For example clang-tidy even has an auto fix mode that could apply
these attributes automatically.

With the attributes in place, we would automatically receive warnings
for every incorrect free attribute, which a tool could then
automatically fix.

If we want to avoid generating noise by placing attributes everywhere
in the source (I'm not sure how noisy that would be), that part could
be a specialized CI run instead, since the transformation itself can
be automated.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2026-05-07 22:01:16 Re: First draft of PG 19 release notes
Previous Message Nathan Bossart 2026-05-07 21:46:08 Re: Fix typo 586/686 in atomics/arch-x86.h