Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cdb41b

Browse files
committed
Ensure plperl strings are always correctly UTF8 encoded.
Amit Khandekar and Alex Hunsaker. Backpatched to 9.1 where the problem first occurred.
1 parent fd6dbc2 commit 4cdb41b

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/pl/plperl/GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ PSQLDIR = $(bindir)
5757

5858
include $(top_srcdir)/src/Makefile.shlib
5959

60-
plperl.o: perlchunks.h plperl_opmask.h
60+
plperl.o: perlchunks.h plperl_opmask.h plperl_helpers.h
6161

6262
plperl_opmask.h: plperl_opmask.pl
6363
@if [ x"$(perl_privlibexp)" = x"" ]; then echo "configure switch --with-perl was not specified."; exit 1; fi

src/pl/plperl/expected/plperl.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,16 @@ CONTEXT: PL/Perl anonymous code block
650650
DO $do$ use warnings FATAL => qw(void) ; my @y; my $x = sort @y; 1; $do$ LANGUAGE plperl;
651651
ERROR: Useless use of sort in scalar context at line 1.
652652
CONTEXT: PL/Perl anonymous code block
653+
--
654+
-- Make sure strings are validated
655+
-- Should fail for all encodings, as nul bytes are never permitted.
656+
--
657+
CREATE OR REPLACE FUNCTION perl_zerob() RETURNS TEXT AS $$
658+
return "abcd\0efg";
659+
$$ LANGUAGE plperlu;
660+
SELECT perl_zerob();
661+
ERROR: invalid byte sequence for encoding "UTF8": 0x00
662+
CONTEXT: PL/Perl function "perl_zerob"
653663
-- make sure functions marked as VOID without an explicit return work
654664
CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
655665
$_SHARED{myquote} = sub {

src/pl/plperl/plperl_helpers.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
static inline char *
88
utf_u2e(const char *utf8_str, size_t len)
99
{
10-
char *ret = (char *) pg_do_encoding_conversion((unsigned char *) utf8_str, len, PG_UTF8, GetDatabaseEncoding());
10+
int enc = GetDatabaseEncoding();
11+
12+
char *ret = (char *) pg_do_encoding_conversion((unsigned char *) utf8_str, len, PG_UTF8, enc);
13+
14+
/*
15+
* when we are a PG_UTF8 or SQL_ASCII database
16+
* pg_do_encoding_conversion() will not do any conversion or
17+
* verification. we need to do it manually instead.
18+
*/
19+
if (enc == PG_UTF8 || enc == PG_SQL_ASCII)
20+
pg_verify_mbstr_len(PG_UTF8, utf8_str, len, false);
1121

1222
if (ret == utf8_str)
1323
ret = pstrdup(ret);
24+
1425
return ret;
1526
}
1627

src/pl/plperl/sql/plperl.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,15 @@ DO $do$ use strict; my $name = "foo"; my $ref = $$name; $do$ LANGUAGE plperl;
423423
-- yields "ERROR: Useless use of sort in scalar context."
424424
DO $do$ use warnings FATAL => qw(void) ; my @y; my $x = sort @y; 1; $do$ LANGUAGE plperl;
425425

426+
--
427+
-- Make sure strings are validated
428+
-- Should fail for all encodings, as nul bytes are never permitted.
429+
--
430+
CREATE OR REPLACE FUNCTION perl_zerob() RETURNS TEXT AS $$
431+
return "abcd\0efg";
432+
$$ LANGUAGE plperlu;
433+
SELECT perl_zerob();
434+
426435
-- make sure functions marked as VOID without an explicit return work
427436
CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
428437
$_SHARED{myquote} = sub {

0 commit comments

Comments
 (0)