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

Commit 1bebfb9

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 5860b22 commit 1bebfb9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

contrib/hstore_plperl/expected/hstore_plperl.out

Lines changed: 19 additions & 0 deletions
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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,26 @@ Datum
4747
plperl_to_hstore(PG_FUNCTION_ARGS)
4848
{
4949
dTHX;
50-
HV *hv = (HV *) SvRV((SV *) PG_GETARG_POINTER(0));
50+
SV *in = (SV *) PG_GETARG_POINTER(0);
51+
HV *hv;
5152
HE *he;
5253
int32 buflen;
5354
int32 i;
5455
int32 pcount;
5556
HStore *out;
5657
Pairs *pairs;
5758

59+
/* Dereference references recursively. */
60+
while (SvROK(in))
61+
in = SvRV(in);
62+
63+
/* Now we must have a hash. */
64+
if (SvTYPE(in) != SVt_PVHV)
65+
ereport(ERROR,
66+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
67+
(errmsg("cannot transform non-hash Perl value to hstore"))));
68+
hv = (HV *) in;
69+
5870
pcount = hv_iterinit(hv);
5971

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

contrib/hstore_plperl/sql/hstore_plperl.sql

Lines changed: 19 additions & 0 deletions
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)