diff --git a/src/pl/plperl/SPI.xs b/src/pl/plperl/SPI.xs index 6b8dcf6..0447c50 100644 --- a/src/pl/plperl/SPI.xs +++ b/src/pl/plperl/SPI.xs @@ -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(); } diff --git a/src/pl/plperl/Util.xs b/src/pl/plperl/Util.xs index b2e0dfc..8c3c47f 100644 --- a/src/pl/plperl/Util.xs +++ b/src/pl/plperl/Util.xs @@ -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(); } diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index d57189f..d6584ab 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -3005,7 +3005,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; @@ -3238,7 +3238,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; @@ -3324,7 +3324,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; @@ -3499,7 +3499,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; @@ -3640,7 +3640,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; @@ -3769,7 +3769,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; diff --git a/src/pl/plperl/plperl_helpers.h b/src/pl/plperl/plperl_helpers.h index c1c7c29..9599e5f 100644 --- a/src/pl/plperl/plperl_helpers.h +++ b/src/pl/plperl/plperl_helpers.h @@ -121,4 +121,35 @@ cstr2sv(const char *str) return sv; } +/* + * croak converting from a string assumed to be in the current database's + * encoding to UTF-8 for croak(ing). + */ +static inline void +croak_cstr(const char *str) +{ +#ifdef croak_sv + croak_sv(sv_2mortal(cstr2sv(str))); +#else + /* + * croak_sv() is not available in older perls and croak() does not play + * nicely with utf8. + */ + char *utf8_str = utf_e2u(str); + SV *errsv = get_sv("@", GV_ADD); + SV *ssv; + + /* + * Add error error location manually, croak() appends it too late. + */ + ssv = mess("%s", utf8_str); + pfree(utf8_str); + + SvUTF8_on(ssv); + sv_setsv(errsv, ssv); + + croak(NULL); +#endif +} + #endif /* PL_PERL_HELPERS_H */