Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e3b7f7c

Browse files
committed
Fix contrib/hstore_plperl to look through scalar refs.
Bring this transform function into sync with the policy established by commit 3a38298. Also, fix it to make sure that what it drills down to is indeed a hash, and not some other kind of Perl SV. Previously, the test cases added here provoked crashes. Because of the crash hazard, back-patch to 9.5 where this module was introduced. Discussion: https://postgr.es/m/28336.1528393969@sss.pgh.pa.us
1 parent 3a38298 commit e3b7f7c

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

contrib/hstore_plperl/expected/hstore_plperl.out

+19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ SELECT test2arr();
4141
{"\"a\"=>\"1\", \"b\"=>\"boo\", \"c\"=>NULL","\"d\"=>\"2\""}
4242
(1 row)
4343

44+
-- check error cases
45+
CREATE OR REPLACE FUNCTION test2() RETURNS hstore
46+
LANGUAGE plperl
47+
TRANSFORM FOR TYPE hstore
48+
AS $$
49+
return 42;
50+
$$;
51+
SELECT test2();
52+
ERROR: cannot transform non-hash Perl value to hstore
53+
CONTEXT: PL/Perl function "test2"
54+
CREATE OR REPLACE FUNCTION test2() RETURNS hstore
55+
LANGUAGE plperl
56+
TRANSFORM FOR TYPE hstore
57+
AS $$
58+
return [1, 2];
59+
$$;
60+
SELECT test2();
61+
ERROR: cannot transform non-hash Perl value to hstore
62+
CONTEXT: PL/Perl function "test2"
4463
DROP FUNCTION test2();
4564
DROP FUNCTION test2arr();
4665
DROP EXTENSION hstore_plperl;

contrib/hstore_plperl/hstore_plperl.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,26 @@ Datum
101101
plperl_to_hstore(PG_FUNCTION_ARGS)
102102
{
103103
dTHX;
104-
HV *hv = (HV *) SvRV((SV *) PG_GETARG_POINTER(0));
104+
SV *in = (SV *) PG_GETARG_POINTER(0);
105+
HV *hv;
105106
HE *he;
106107
int32 buflen;
107108
int32 i;
108109
int32 pcount;
109110
HStore *out;
110111
Pairs *pairs;
111112

113+
/* Dereference references recursively. */
114+
while (SvROK(in))
115+
in = SvRV(in);
116+
117+
/* Now we must have a hash. */
118+
if (SvTYPE(in) != SVt_PVHV)
119+
ereport(ERROR,
120+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
121+
(errmsg("cannot transform non-hash Perl value to hstore"))));
122+
hv = (HV *) in;
123+
112124
pcount = hv_iterinit(hv);
113125

114126
pairs = palloc(pcount * sizeof(Pairs));

contrib/hstore_plperl/sql/hstore_plperl.sql

+19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ $$;
3131

3232
SELECT test2arr();
3333

34+
-- check error cases
35+
CREATE OR REPLACE FUNCTION test2() RETURNS hstore
36+
LANGUAGE plperl
37+
TRANSFORM FOR TYPE hstore
38+
AS $$
39+
return 42;
40+
$$;
41+
42+
SELECT test2();
43+
44+
CREATE OR REPLACE FUNCTION test2() RETURNS hstore
45+
LANGUAGE plperl
46+
TRANSFORM FOR TYPE hstore
47+
AS $$
48+
return [1, 2];
49+
$$;
50+
51+
SELECT test2();
52+
3453

3554
DROP FUNCTION test2();
3655
DROP FUNCTION test2arr();

0 commit comments

Comments
 (0)