Andres Freund <and...@anarazel.de> writes: > Hi, > > On 2025-02-12 11:02:04 -0500, Tom Lane wrote: >> I wish we had some way to detect misuses automatically ... >> >> This seems like the sort of bug that Coverity could detect if only it >> knew to look, but I have no idea if it could be configured that way. >> Maybe some weird lashup with a debugging malloc library would be >> another way. > > Recent gcc actually has a fairly good way to detect this kind of issue: > https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute > in particular, the variant of the attribute with "deallocator".
[...] > Note that that doesn't just require adding the attributes to > PQescapeIdentifier() etc, but also to pg_malloc(), as the latter is how gcc > knows that pg_pfree() is a deallocating function. > > > Particularly for something like libpq it's not quitetrivial to add > attributes like this, of course. We can't even depend on pg_config.h. > > One way would be to define them in libpq-fe.h, guarded by an #ifdef, that's > "armed" by a commandline -D flag, if the compiler is supported? Does it need a -D flag, wouldn't __has_attribute(malloc) (with the fallback definition in c.h) be enough? - ilmari