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 2dfa15de5).

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
This commit is contained in:
Tom Lane 2015-09-29 10:52:22 -04:00
parent 758fcfdc01
commit b631a46ed8
7 changed files with 87 additions and 8 deletions

View File

@ -41,7 +41,7 @@ do_plperl_return_next(SV *sv)
FlushErrorState();
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
}
PG_END_TRY();
}

View File

@ -58,7 +58,7 @@ do_util_elog(int level, SV *msg)
pfree(cmsg);
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
}
PG_END_TRY();
}

View File

@ -97,3 +97,16 @@ NOTICE: caught die
2
(1 row)
-- Test non-ASCII error messages
--
-- Note: this test case is known to fail if the database encoding is
-- EUC_CN, EUC_JP, EUC_KR, or EUC_TW, for lack of any equivalent to
-- U+00A0 (no-break space) in those encodings. However, testing with
-- plain ASCII data would be rather useless, so we must live with that.
SET client_encoding TO UTF8;
create or replace function error_with_nbsp() returns void language plperl as $$
elog(ERROR, "this message contains a no-break space");
$$;
select error_with_nbsp();
ERROR: this message contains a no-break space at line 2.
CONTEXT: PL/Perl function "error_with_nbsp"

View File

@ -97,3 +97,16 @@ NOTICE: caught die
2
(1 row)
-- Test non-ASCII error messages
--
-- Note: this test case is known to fail if the database encoding is
-- EUC_CN, EUC_JP, EUC_KR, or EUC_TW, for lack of any equivalent to
-- U+00A0 (no-break space) in those encodings. However, testing with
-- plain ASCII data would be rather useless, so we must live with that.
SET client_encoding TO UTF8;
create or replace function error_with_nbsp() returns void language plperl as $$
elog(ERROR, "this message contains a no-break space");
$$;
select error_with_nbsp();
ERROR: this message contains a no-break space at line 2.
CONTEXT: PL/Perl function "error_with_nbsp"

View File

@ -3066,7 +3066,7 @@ plperl_spi_exec(char *query, int limit)
SPI_restore_connection();
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
/* Can't get here, but keep compiler quiet */
return NULL;
@ -3299,7 +3299,7 @@ plperl_spi_query(char *query)
SPI_restore_connection();
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
/* Can't get here, but keep compiler quiet */
return NULL;
@ -3385,7 +3385,7 @@ plperl_spi_fetchrow(char *cursor)
SPI_restore_connection();
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
/* Can't get here, but keep compiler quiet */
return NULL;
@ -3560,7 +3560,7 @@ plperl_spi_prepare(char *query, int argc, SV **argv)
SPI_restore_connection();
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
/* Can't get here, but keep compiler quiet */
return NULL;
@ -3701,7 +3701,7 @@ plperl_spi_exec_prepared(char *query, HV *attr, int argc, SV **argv)
SPI_restore_connection();
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
/* Can't get here, but keep compiler quiet */
return NULL;
@ -3830,7 +3830,7 @@ plperl_spi_query_prepared(char *query, int argc, SV **argv)
SPI_restore_connection();
/* Punt the error to Perl */
croak("%s", edata->message);
croak_cstr(edata->message);
/* Can't get here, but keep compiler quiet */
return NULL;

View File

@ -123,4 +123,42 @@ cstr2sv(const char *str)
return sv;
}
/*
* croak() with specified message, which is given in the database encoding.
*
* Ideally we'd just write croak("%s", str), but plain croak() does not play
* nice with non-ASCII data. In modern Perl versions we can call cstr2sv()
* and pass the result to croak_sv(); in versions that don't have croak_sv(),
* we have to work harder.
*/
static inline void
croak_cstr(const char *str)
{
#ifdef croak_sv
/* Use sv_2mortal() to be sure the transient SV gets freed */
croak_sv(sv_2mortal(cstr2sv(str)));
#else
/*
* The older way to do this is to assign a UTF8-marked value to ERRSV and
* then call croak(NULL). But if we leave it to croak() to append the
* error location, it does so too late (only after popping the stack) in
* some Perl versions. Hence, use mess() to create an SV with the error
* location info already appended.
*/
SV *errsv = get_sv("@", GV_ADD);
char *utf8_str = utf_e2u(str);
SV *ssv;
ssv = mess("%s", utf8_str);
SvUTF8_on(ssv);
pfree(utf8_str);
sv_setsv(errsv, ssv);
croak(NULL);
#endif /* croak_sv */
}
#endif /* PL_PERL_HELPERS_H */

View File

@ -76,3 +76,18 @@ return $a + $b;
$$;
select indirect_die_caller();
-- Test non-ASCII error messages
--
-- Note: this test case is known to fail if the database encoding is
-- EUC_CN, EUC_JP, EUC_KR, or EUC_TW, for lack of any equivalent to
-- U+00A0 (no-break space) in those encodings. However, testing with
-- plain ASCII data would be rather useless, so we must live with that.
SET client_encoding TO UTF8;
create or replace function error_with_nbsp() returns void language plperl as $$
elog(ERROR, "this message contains a no-break space");
$$;
select error_with_nbsp();