Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfc661f7

Browse files
committed
plperl: Skip setting UTF8 flag when in SQL_ASCII encoding
When in SQL_ASCII encoding, strings passed around are not necessarilyUTF8-safe. We had already fixed this in some places, but it looks likewe missed some.I had to backpatch Peter Eisentraut'sa8b92b6 to 9.1 in order for thispatch to cherry-pick more cleanly.Patch from Alex Hunsaker, tweaked by Kyotaro HORIGUCHI and myself.Some desultory cleanup and comment addition by me, during patch review.Per bug report from Christoph Berg in20120209102116.GA14429@msgid.df7cb.de
1 parent1fbe7d3 commitfc661f7

File tree

5 files changed

+71
-47
lines changed

5 files changed

+71
-47
lines changed

‎src/pl/plperl/GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ PERLCHUNKS = plc_perlboot.pl plc_trusted.pl
4444
SHLIB_LINK =$(perl_embed_ldflags)
4545

4646
REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-extension=plperl --load-extension=plperlu
47-
REGRESS = plperl plperl_trigger plperl_shared plperl_elog plperl_util plperl_init plperlu plperl_array
47+
REGRESS = plperlplperl_lcplperl_trigger plperl_shared plperl_elog plperl_util plperl_init plperlu plperl_array
4848
# if Perl can support two interpreters in one backend,
4949
# test plperl-and-plperlu cases
5050
ifneq ($(PERL),)

‎src/pl/plperl/Util.xs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ static text *
6767
sv2text(SV*sv)
6868
{
6969
char*str=sv2cstr(sv);
70+
text*text;
7071

71-
returncstring_to_text(str);
72+
text=cstring_to_text(str);
73+
pfree(str);
74+
returntext;
7275
}
7376

7477
MODULE=PostgreSQL::InServer::UtilPREFIX=util_
@@ -113,8 +116,11 @@ util_quote_literal(sv)
113116
}
114117
else {
115118
text*arg=sv2text(sv);
116-
text*ret=DatumGetTextP(DirectFunctionCall1(quote_literal,PointerGetDatum(arg)));
117-
char*str=text_to_cstring(ret);
119+
text*quoted=DatumGetTextP(DirectFunctionCall1(quote_literal,PointerGetDatum(arg)));
120+
char*str;
121+
122+
pfree(arg);
123+
str=text_to_cstring(quoted);
118124
RETVAL=cstr2sv(str);
119125
pfree(str);
120126
}
@@ -132,8 +138,11 @@ util_quote_nullable(sv)
132138
else
133139
{
134140
text*arg=sv2text(sv);
135-
text*ret=DatumGetTextP(DirectFunctionCall1(quote_nullable,PointerGetDatum(arg)));
136-
char*str=text_to_cstring(ret);
141+
text*quoted=DatumGetTextP(DirectFunctionCall1(quote_nullable,PointerGetDatum(arg)));
142+
char*str;
143+
144+
pfree(arg);
145+
str=text_to_cstring(quoted);
137146
RETVAL=cstr2sv(str);
138147
pfree(str);
139148
}
@@ -145,12 +154,14 @@ util_quote_ident(sv)
145154
SV*sv
146155
PREINIT:
147156
text*arg;
148-
text*ret;
157+
text*quoted;
149158
char*str;
150159
CODE:
151160
arg=sv2text(sv);
152-
ret=DatumGetTextP(DirectFunctionCall1(quote_ident,PointerGetDatum(arg)));
153-
str=text_to_cstring(ret);
161+
quoted=DatumGetTextP(DirectFunctionCall1(quote_ident,PointerGetDatum(arg)));
162+
163+
pfree(arg);
164+
str=text_to_cstring(quoted);
154165
RETVAL=cstr2sv(str);
155166
pfree(str);
156167
OUTPUT:

‎src/pl/plperl/expected/plperl.out

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -650,16 +650,6 @@ 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 plperl;
660-
SELECT perl_zerob();
661-
ERROR: invalid byte sequence for encoding "UTF8": 0x00
662-
CONTEXT: PL/Perl function "perl_zerob"
663653
-- make sure functions marked as VOID without an explicit return work
664654
CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
665655
$_SHARED{myquote} = sub {

‎src/pl/plperl/plperl_helpers.h

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@
33

44
/*
55
* convert from utf8 to database encoding
6+
*
7+
* Returns a palloc'ed copy of the original string
68
*/
79
staticinlinechar*
8-
utf_u2e(constchar*utf8_str,size_tlen)
10+
utf_u2e(char*utf8_str,size_tlen)
911
{
10-
intenc=GetDatabaseEncoding();
11-
12-
char*ret= (char*)pg_do_encoding_conversion((unsignedchar*)utf8_str,len,PG_UTF8,enc);
12+
intenc=GetDatabaseEncoding();
13+
char*ret;
1314

1415
/*
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-
*/
16+
* When we are in a PG_UTF8 or SQL_ASCII database
17+
* pg_do_encoding_conversion() will not do any conversion (which is good)
18+
* or verification (not so much), so we need to run the verification step
19+
* separately.
20+
*/
1921
if (enc==PG_UTF8||enc==PG_SQL_ASCII)
20-
pg_verify_mbstr_len(PG_UTF8,utf8_str,len, false);
22+
{
23+
pg_verify_mbstr_len(enc,utf8_str,len, false);
24+
ret=utf8_str;
25+
}
26+
else
27+
ret= (char*)pg_do_encoding_conversion((unsignedchar*)utf8_str,
28+
len,PG_UTF8,enc);
2129

2230
if (ret==utf8_str)
2331
ret=pstrdup(ret);
@@ -27,11 +35,15 @@ utf_u2e(const char *utf8_str, size_t len)
2735

2836
/*
2937
* convert from database encoding to utf8
38+
*
39+
* Returns a palloc'ed copy of the original string
3040
*/
3141
staticinlinechar*
3242
utf_e2u(constchar*str)
3343
{
34-
char*ret= (char*)pg_do_encoding_conversion((unsignedchar*)str,strlen(str),GetDatabaseEncoding(),PG_UTF8);
44+
char*ret=
45+
(char*)pg_do_encoding_conversion((unsignedchar*)str,strlen(str),
46+
GetDatabaseEncoding(),PG_UTF8);
3547

3648
if (ret==str)
3749
ret=pstrdup(ret);
@@ -41,6 +53,8 @@ utf_e2u(const char *str)
4153

4254
/*
4355
* Convert an SV to a char * in the current database encoding
56+
*
57+
* Returns a palloc'ed copy of the original string
4458
*/
4559
staticinlinechar*
4660
sv2cstr(SV*sv)
@@ -50,7 +64,9 @@ sv2cstr(SV *sv)
5064

5165
/*
5266
* get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
53-
*
67+
*/
68+
69+
/*
5470
* SvPVutf8() croaks nastily on certain things, like typeglobs and
5571
* readonly objects such as $^V. That's a perl bug - it's not supposed to
5672
* happen. To avoid crashing the backend, we make a copy of the sv before
@@ -62,15 +78,27 @@ sv2cstr(SV *sv)
6278
(SvTYPE(sv)>SVt_PVLV&&SvTYPE(sv)!=SVt_PVFM))
6379
sv=newSVsv(sv);
6480
else
65-
/* increase the reference count so we cant just SvREFCNT_dec() it when
66-
* we are done */
81+
{
82+
/*
83+
* increase the reference count so we can just SvREFCNT_dec() it when
84+
* we are done
85+
*/
6786
SvREFCNT_inc_simple_void(sv);
87+
}
6888

69-
val=SvPVutf8(sv,len);
89+
/*
90+
* Request the string from Perl, in UTF-8 encoding; but if we're in a
91+
* SQL_ASCII database, just request the byte soup without trying to make it
92+
* UTF8, because that might fail.
93+
*/
94+
if (GetDatabaseEncoding()==PG_SQL_ASCII)
95+
val=SvPV(sv,len);
96+
else
97+
val=SvPVutf8(sv,len);
7098

7199
/*
72-
*weuse perl's length in the event we had an embedded null byte to ensure
73-
* we error out properly
100+
*Now convert to database encoding. Weuse perl's length in the event we
101+
*had an embedded null byte to ensurewe error out properly.
74102
*/
75103
res=utf_u2e(val,len);
76104

@@ -84,16 +112,20 @@ sv2cstr(SV *sv)
84112
* Create a new SV from a string assumed to be in the current database's
85113
* encoding.
86114
*/
87-
88115
staticinlineSV*
89116
cstr2sv(constchar*str)
90117
{
91118
SV*sv;
92-
char*utf8_str=utf_e2u(str);
119+
char*utf8_str;
120+
121+
/* no conversion when SQL_ASCII */
122+
if (GetDatabaseEncoding()==PG_SQL_ASCII)
123+
returnnewSVpv(str,0);
124+
125+
utf8_str=utf_e2u(str);
93126

94127
sv=newSVpv(utf8_str,0);
95128
SvUTF8_on(sv);
96-
97129
pfree(utf8_str);
98130

99131
returnsv;

‎src/pl/plperl/sql/plperl.sql

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,6 @@ 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 REPLACEFUNCTIONperl_zerob() RETURNSTEXTAS $$
431-
return"abcd\0efg";
432-
$$ LANGUAGE plperl;
433-
SELECT perl_zerob();
434-
435426
-- make sure functions marked as VOID without an explicit return work
436427
CREATE OR REPLACEFUNCTIONmyfuncs() RETURNS voidAS $$
437428
$_SHARED{myquote}= sub {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp