Skip to content

Commit f60b2e2

Browse files
committed
Fix plperl to handle non-ASCII error message texts correctly.
We were passing error message texts to croak() verbatim, which turns out not to work if the text contains non-ASCII characters; Perl mangles their encoding, as reported in bug #13638 from Michal Leinweber. To fix, convert the text into a UTF8-encoded SV first. It's hard to test this without risking failures in different database encodings; but we can follow the lead of plpython, which is already assuming that no-break space (U+00A0) has an equivalent in all encodings we care about running the regression tests in (cf commit 2dfa15d). Back-patch to 9.1. The code is quite different in 9.0, and anyway it seems too risky to put something like this into 9.0's final minor release. Alex Hunsaker, with suggestions from Tim Bunce and Tom Lane
1 parent 729d005 commit f60b2e2

File tree

7 files changed

+87
-8
lines changed

7 files changed

+87
-8
lines changed

src/pl/plperl/SPI.xs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ do_plperl_return_next(SV *sv)
4141
FlushErrorState();
4242

4343
/* Punt the error to Perl */
44-
croak("%s", edata->message);
44+
croak_cstr(edata->message);
4545
}
4646
PG_END_TRY();
4747
}

src/pl/plperl/Util.xs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ do_util_elog(int level, SV *msg)
5858
pfree(cmsg);
5959

6060
/* Punt the error to Perl */
61-
croak("%s", edata->message);
61+
croak_cstr(edata->message);
6262
}
6363
PG_END_TRY();
6464
}

src/pl/plperl/expected/plperl_elog.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,16 @@ PL/Perl function "indirect_die_caller"
104104
2
105105
(1 row)
106106

107+
-- Test non-ASCII error messages
108+
--
109+
-- Note: this test case is known to fail if the database encoding is
110+
-- EUC_CN, EUC_JP, EUC_KR, or EUC_TW, for lack of any equivalent to
111+
-- U+00A0 (no-break space) in those encodings. However, testing with
112+
-- plain ASCII data would be rather useless, so we must live with that.
113+
SET client_encoding TO UTF8;
114+
create or replace function error_with_nbsp() returns void language plperl as $$
115+
elog(ERROR, "this message contains a no-break space");
116+
$$;
117+
select error_with_nbsp();
118+
ERROR: this message contains a no-break space at line 2.
119+
CONTEXT: PL/Perl function "error_with_nbsp"

src/pl/plperl/expected/plperl_elog_1.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,16 @@ PL/Perl function "indirect_die_caller"
104104
2
105105
(1 row)
106106

107+
-- Test non-ASCII error messages
108+
--
109+
-- Note: this test case is known to fail if the database encoding is
110+
-- EUC_CN, EUC_JP, EUC_KR, or EUC_TW, for lack of any equivalent to
111+
-- U+00A0 (no-break space) in those encodings. However, testing with
112+
-- plain ASCII data would be rather useless, so we must live with that.
113+
SET client_encoding TO UTF8;
114+
create or replace function error_with_nbsp() returns void language plperl as $$
115+
elog(ERROR, "this message contains a no-break space");
116+
$$;
117+
select error_with_nbsp();
118+
ERROR: this message contains a no-break space at line 2.
119+
CONTEXT: PL/Perl function "error_with_nbsp"

src/pl/plperl/plperl.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,7 +2885,7 @@ plperl_spi_exec(char *query, int limit)
28852885
SPI_restore_connection();
28862886

28872887
/* Punt the error to Perl */
2888-
croak("%s", edata->message);
2888+
croak_cstr(edata->message);
28892889

28902890
/* Can't get here, but keep compiler quiet */
28912891
return NULL;
@@ -3118,7 +3118,7 @@ plperl_spi_query(char *query)
31183118
SPI_restore_connection();
31193119

31203120
/* Punt the error to Perl */
3121-
croak("%s", edata->message);
3121+
croak_cstr(edata->message);
31223122

31233123
/* Can't get here, but keep compiler quiet */
31243124
return NULL;
@@ -3204,7 +3204,7 @@ plperl_spi_fetchrow(char *cursor)
32043204
SPI_restore_connection();
32053205

32063206
/* Punt the error to Perl */
3207-
croak("%s", edata->message);
3207+
croak_cstr(edata->message);
32083208

32093209
/* Can't get here, but keep compiler quiet */
32103210
return NULL;
@@ -3379,7 +3379,7 @@ plperl_spi_prepare(char *query, int argc, SV **argv)
33793379
SPI_restore_connection();
33803380

33813381
/* Punt the error to Perl */
3382-
croak("%s", edata->message);
3382+
croak_cstr(edata->message);
33833383

33843384
/* Can't get here, but keep compiler quiet */
33853385
return NULL;
@@ -3520,7 +3520,7 @@ plperl_spi_exec_prepared(char *query, HV *attr, int argc, SV **argv)
35203520
SPI_restore_connection();
35213521

35223522
/* Punt the error to Perl */
3523-
croak("%s", edata->message);
3523+
croak_cstr(edata->message);
35243524

35253525
/* Can't get here, but keep compiler quiet */
35263526
return NULL;
@@ -3649,7 +3649,7 @@ plperl_spi_query_prepared(char *query, int argc, SV **argv)
36493649
SPI_restore_connection();
36503650

36513651
/* Punt the error to Perl */
3652-
croak("%s", edata->message);
3652+
croak_cstr(edata->message);
36533653

36543654
/* Can't get here, but keep compiler quiet */
36553655
return NULL;

src/pl/plperl/plperl_helpers.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,42 @@ cstr2sv(const char *str)
132132
return sv;
133133
}
134134

135+
/*
136+
* croak() with specified message, which is given in the database encoding.
137+
*
138+
* Ideally we'd just write croak("%s", str), but plain croak() does not play
139+
* nice with non-ASCII data. In modern Perl versions we can call cstr2sv()
140+
* and pass the result to croak_sv(); in versions that don't have croak_sv(),
141+
* we have to work harder.
142+
*/
143+
static inline void
144+
croak_cstr(const char *str)
145+
{
146+
#ifdef croak_sv
147+
/* Use sv_2mortal() to be sure the transient SV gets freed */
148+
croak_sv(sv_2mortal(cstr2sv(str)));
149+
#else
150+
151+
/*
152+
* The older way to do this is to assign a UTF8-marked value to ERRSV and
153+
* then call croak(NULL). But if we leave it to croak() to append the
154+
* error location, it does so too late (only after popping the stack) in
155+
* some Perl versions. Hence, use mess() to create an SV with the error
156+
* location info already appended.
157+
*/
158+
SV *errsv = get_sv("@", GV_ADD);
159+
char *utf8_str = utf_e2u(str);
160+
SV *ssv;
161+
162+
ssv = mess("%s", utf8_str);
163+
SvUTF8_on(ssv);
164+
165+
pfree(utf8_str);
166+
167+
sv_setsv(errsv, ssv);
168+
169+
croak(NULL);
170+
#endif /* croak_sv */
171+
}
172+
135173
#endif /* PL_PERL_HELPERS_H */

src/pl/plperl/sql/plperl_elog.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,18 @@ return $a + $b;
7676
$$;
7777

7878
select indirect_die_caller();
79+
80+
-- Test non-ASCII error messages
81+
--
82+
-- Note: this test case is known to fail if the database encoding is
83+
-- EUC_CN, EUC_JP, EUC_KR, or EUC_TW, for lack of any equivalent to
84+
-- U+00A0 (no-break space) in those encodings. However, testing with
85+
-- plain ASCII data would be rather useless, so we must live with that.
86+
87+
SET client_encoding TO UTF8;
88+
89+
create or replace function error_with_nbsp() returns void language plperl as $$
90+
elog(ERROR, "this message contains a no-break space");
91+
$$;
92+
93+
select error_with_nbsp();

0 commit comments

Comments
 (0)