Hold a reference on a Perl array or hash while scanning it. Previously we assumed that the object would stick around while we examined it, but there are a few ways that the last refcount could get dropped meanwhile, allowing the object to be recycled and creating a use-after-free hazard. To prevent that, we must acquire our own refcount and release it when we're done. The tricky part is to ensure that the refcount gets released if an error is thrown partway through. A TRY/FINALLY block would work, but that answer would be expensive and notationally invasive. The method used here is to push a refcount-carrying RV onto Perl's temporaries stack so that temporaries cleanup will release the reference. A simple benchmark suggests that that adds just a couple percent of overhead, which seems acceptable.
The first draft of this patch was created by Anthropic's OSS program. Reported-by: Anthropic OSS program Author: Tom Lane <[email protected]> Co-authored-by: Anthropic OSS program Backpatch-through: 14 Security: CVE-2026-116320 Branch ------ REL_15_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/b9ad255648fc96a3c4c8d9a64bb751949249abb6 Author: Tom Lane <[email protected]> Modified Files -------------- contrib/hstore_plperl/hstore_plperl.c | 9 ++++ contrib/jsonb_plperl/jsonb_plperl.c | 22 ++++++++- src/pl/plperl/plperl.c | 84 +++++++++++++++++++++++++++++++---- 3 files changed, 106 insertions(+), 9 deletions(-)
