pgsql: Avoid memory leak in validation of a PL/Python trigger function.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Avoid memory leak in validation of a PL/Python trigger function.
Date: 2025-10-23 18:23:31
Message-ID: E1vBzyF-0039wc-2y@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Avoid memory leak in validation of a PL/Python trigger function.

If we're trying to perform check_function_bodies validation
of a PL/Python trigger function, we create a new PLyProcedure,
but we don't put it into the PLy_procedure_cache hash table.
(Doing so would be useless, since we don't have the relation
OID that is part of the cache key for a trigger function, so
we could not make an entry that would be found by later uses.)
However, we didn't think through what to do instead, with the
result that the PLyProcedure was simply leaked.

It would take a pretty large number of CREATE FUNCTION operations
for this to amount to a serious problem, but it's easy to see the
memory bloat if you do CREATE OR REPLACE FUNCTION in a loop.

To fix, have PLy_procedure_get delete the new PLyProcedure
and return NULL if it's not going to cache the PLyProcedure.
I considered making plpython3_validator do the cleanup instead,
which would be more natural. But then plpython3_validator would
have to know the rules under which PLy_procedure_get returns a
non-cached PLyProcedure, else it risks deleting something that's
pointed to by a cache entry. On the whole it seems more robust
to deal with the case inside PLy_procedure_get.

Found by the new version of Coverity (nice catch!). In the end
I feel this fix is more about satisfying Coverity than about
fixing a real-world problem, so I'm not going to back-patch.

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/e557c82759a3362d8fb618c798ca99ee90be96c5

Modified Files
--------------
src/pl/plpython/plpy_main.c | 2 +-
src/pl/plpython/plpy_procedure.c | 13 ++++++++++++-
2 files changed, 13 insertions(+), 2 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message David Rowley 2025-10-23 18:43:09 Re: pgsql: Use CompactAttribute more often, when possible
Previous Message Tom Lane 2025-10-23 16:33:18 pgsql: Fix off-by-one Asserts in FreePageBtreeInsertInternal/Leaf.