]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
plperl: Fix NULL pointer dereference for forged array object
authorRichard Guo <rguo@postgresql.org>
Wed, 24 Jun 2026 00:09:48 +0000 (09:09 +0900)
committerRichard Guo <rguo@postgresql.org>
Wed, 24 Jun 2026 00:16:24 +0000 (09:16 +0900)
In get_perl_array_ref(), for a PostgreSQL::InServer::ARRAY object, we
look up its "array" key with hv_fetch_string() and then inspect the
returned SV.  However, hv_fetch_string() returns a NULL pointer when
the key is absent, and the code dereferenced that result without first
checking whether the pointer itself was NULL.  As a result, a plperl
function returning a forged PostgreSQL::InServer::ARRAY object that
lacks the "array" key would crash the backend with a segmentation
fault.

Fix this by checking the pointer returned by hv_fetch_string() before
dereferencing it, matching how other callers in this file already
guard the result.  With the check in place, such an object falls
through to the existing error report instead of crashing.

Author: Xing Guo <higuoxing@gmail.com>
Reviewed-by: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CACpMh+DYgcnqZwQLXXuxQcehJTd7T8UmKWSLsK4mFBEp9G2ajA@mail.gmail.com
Backpatch-through: 14

src/pl/plperl/expected/plperl_array.out
src/pl/plperl/plperl.c
src/pl/plperl/sql/plperl_array.sql

index bd04a062fb9de4cb293c09972142eaf3ec282b4a..03e5629d699c4e8efd1b2b3f7c8a97fad87c3d09 100644 (file)
@@ -274,3 +274,10 @@ select perl_setof_array('{{1}, {2}, {3}}');
  {3}
 (3 rows)
 
+-- Test a forged PostgreSQL::InServer::ARRAY object lacking the 'array' key
+CREATE OR REPLACE FUNCTION perl_forged_array() RETURNS integer[] AS $$
+       return bless {}, "PostgreSQL::InServer::ARRAY";
+$$ LANGUAGE plperl;
+SELECT perl_forged_array();
+ERROR:  could not get array reference from PostgreSQL::InServer::ARRAY object
+CONTEXT:  PL/Perl function "perl_forged_array"
index 58b77256255b80bac65d0f140648b47f302cdeae..26ba25e46cef0710166d1543e0c6f3649463a42e 100644 (file)
@@ -1151,7 +1151,7 @@ get_perl_array_ref(SV *sv)
                        HV                 *hv = (HV *) SvRV(sv);
                        SV                **sav = hv_fetch_string(hv, "array");
 
-                       if (*sav && SvOK(*sav) && SvROK(*sav) &&
+                       if (sav && *sav && SvOK(*sav) && SvROK(*sav) &&
                                SvTYPE(SvRV(*sav)) == SVt_PVAV)
                                return *sav;
 
index ca63b5db62593260022f4b9efb6a1a1198f134d3..cd1d7e34c5078c82fd93f00e9904fee8ec6c6966 100644 (file)
@@ -206,3 +206,10 @@ create or replace function perl_setof_array(integer[]) returns setof integer[] l
 $$;
 
 select perl_setof_array('{{1}, {2}, {3}}');
+
+-- Test a forged PostgreSQL::InServer::ARRAY object lacking the 'array' key
+CREATE OR REPLACE FUNCTION perl_forged_array() RETURNS integer[] AS $$
+       return bless {}, "PostgreSQL::InServer::ARRAY";
+$$ LANGUAGE plperl;
+
+SELECT perl_forged_array();