pgsql: Defend against null "SV *" pointers in plperl modules. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Defend against null "SV *" pointers in plperl modules.
Date
Msg-id E1wwRRs-000000017Dc-1MIT@gemulon.postgresql.org
Whole thread
List pgsql-committers
Defend against null "SV *" pointers in plperl modules.

Tied hashes, and probably tied arrays, are capable of returning Perl
value pointers that are actually NULL, not the usual pointer to an
undef SV.  We were not defending against that everywhere, leading
to possible SIGSEGV.  Fix the code to consistently treat a null
pointer returned from hv_iternext or av_fetch like a !SvOK one.
(Note that the large diff in SV_to_JsonbValue is actually quite
trivial, but it required reindenting a chunk of existing code.)

Claude Code found the instance in hstore_plperl, and I found the
others by code auditing.  Perhaps the other instances aren't
actually reachable, but I see little reason to assume that.

The known test cases for these errors require perl's Tie modules,
which may not be present, so it doesn't seem worth the trouble
to create regression test cases that would cover them.

Reported-by: Claude Code (via Noah Misch)
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/569769.1786901901@sss.pgh.pa.us
Backpatch-through: 14

Branch
------
REL_19_STABLE

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

Modified Files
--------------
contrib/hstore_plperl/hstore_plperl.c |   2 +-
contrib/jsonb_plperl/jsonb_plperl.c   | 161 ++++++++++++++++++----------------
src/pl/plperl/plperl.c                |   2 +-
3 files changed, 86 insertions(+), 79 deletions(-)


pgsql-committers by date:

Previous
From: Masahiko Sawada
Date:
Subject: pgsql: test_decoding: Don't print virtual generated columns.
Next
From: Peter Geoghegan
Date:
Subject: pgsql: Fix GIN multiple-VACUUM-scans pending list bug.