pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13638: Exception texts from plperl has bad encoding
15+ messages / 6 participants
[nested] [flat]

* BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-25 11:23  lei@aswsyst.cz
  0 siblings, 1 reply; 15+ messages in thread

From: lei@aswsyst.cz @ 2015-09-25 11:23 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13638
Logged by:          Michal Leinweber
Email address:      lei@aswsyst.cz
PostgreSQL version: 9.4.4
Operating system:   Debian 8
Description:        

I have UTF-8 database and using UTF-8 text in plpgsql and plperl functions.
Everything works ok, only exceptions from plperl functions have bad encoding
(maybe double encoded).

Compare output of these 2 functions:

create or replace function perl_test() returns text
language plperl as $$
  return "Český text ěščřžýáíé";
$$;

create or replace function perl_test_err() returns text
language plperl as $$
  elog(ERROR, "Česká chyba ěščřžýáíé");
$$;



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-25 19:18  Tom Lane <tgl@sss.pgh.pa.us>
  parent: lei@aswsyst.cz
  0 siblings, 1 reply; 15+ messages in thread

From: Tom Lane @ 2015-09-25 19:18 UTC (permalink / raw)
  To: lei@aswsyst.cz; +Cc: pgsql-bugs

lei@aswsyst.cz writes:
> I have UTF-8 database and using UTF-8 text in plpgsql and plperl functions.
> Everything works ok, only exceptions from plperl functions have bad encoding
> (maybe double encoded).

> Compare output of these 2 functions:

> create or replace function perl_test() returns text
> language plperl as $$
>   return "Český text ěščřžýáíé";
> $$;

> create or replace function perl_test_err() returns text
> language plperl as $$
>   elog(ERROR, "Česká chyba ěščřžýáíé");
> $$;

I traced through this example to the extent of finding that:

* The string passed to elog() in do_util_elog() seems correctly
encoded.

* So does the string passed to croak() after elog does its longjmp,
which is unsurprising since elog.c doesn't really do anything to it.

* Back in plperl_call_perl_func, we use sv2cstr(ERRSV) to get the
error string.  sv2cstr calls "SvPVutf8(sv, len)", and that is what
is giving back the bogusly-encoded data.

I suspect the root problem is that instead of baldly doing

		croak("%s", edata->message);

in do_util_elog(), we need to do something to inform Perl what encoding
the message string is in.  This is beyond my Perl-fu, however.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-25 19:33  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 15+ messages in thread

From: Tom Lane @ 2015-09-25 19:33 UTC (permalink / raw)
  To: lei@aswsyst.cz; +Cc: pgsql-bugs

I wrote:
> I suspect the root problem is that instead of baldly doing
> 		croak("%s", edata->message);
> in do_util_elog(), we need to do something to inform Perl what encoding
> the message string is in.  This is beyond my Perl-fu, however.

BTW, if the answer to that involves turning the message back into an SV,
I think we should not do that but just use the SV that was passed in
originally.  Then the TRY/CATCH could go away entirely, ie the code
would become roughly like

	if (level < ERROR)
	{
		cmsg = sv2cstr(msg);
		elog(level, "%s", cmsg);
		pfree(cmsg);
	}
	else
	{
		croak-with-SV(msg);
	}

which knows slightly more about the meaning of "level" than before,
but otherwise seems much less entangled with anything.

It's still beyond my Perl-fu ...

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-25 19:51  Alvaro Herrera <alvherre@2ndquadrant.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 15+ messages in thread

From: Alvaro Herrera @ 2015-09-25 19:51 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: lei@aswsyst.cz, pgsql-bugs@postgresql.org, Alex Hunsaker <badalex@gmail.com>; Tim Bunce <Tim.Bunce@pobox.com>

Tom Lane wrote:

> It's still beyond my Perl-fu ...

Maybe Alex or Tim can shed some light?  Please see:
http://www.postgresql.org/message-id/flat/20150925112345.26913.28407@wrigleys.postgresql.org

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-25 20:15  Michal Leinweber <lei@aswsyst.cz>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 15+ messages in thread

From: Michal Leinweber @ 2015-09-25 20:15 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

The problem is not only with elog call, but it also fires if plpgsql 
code called from plperl function raises exception.

Best Regards,
     Michal Leinweber

On 2015-09-25 21:33, Tom Lane wrote:
> I wrote:
>> I suspect the root problem is that instead of baldly doing
>> 		croak("%s", edata->message);
>> in do_util_elog(), we need to do something to inform Perl what 
>> encoding
>> the message string is in.  This is beyond my Perl-fu, however.
> 
> BTW, if the answer to that involves turning the message back into an 
> SV,
> I think we should not do that but just use the SV that was passed in
> originally.  Then the TRY/CATCH could go away entirely, ie the code
> would become roughly like
> 
> 	if (level < ERROR)
> 	{
> 		cmsg = sv2cstr(msg);
> 		elog(level, "%s", cmsg);
> 		pfree(cmsg);
> 	}
> 	else
> 	{
> 		croak-with-SV(msg);
> 	}
> 
> which knows slightly more about the meaning of "level" than before,
> but otherwise seems much less entangled with anything.
> 
> It's still beyond my Perl-fu ...
> 
> 			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-25 20:44  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michal Leinweber <lei@aswsyst.cz>
  0 siblings, 0 replies; 15+ messages in thread

From: Tom Lane @ 2015-09-25 20:44 UTC (permalink / raw)
  To: Michal Leinweber <lei@aswsyst.cz>; +Cc: pgsql-bugs

Michal Leinweber <lei@aswsyst.cz> writes:
> The problem is not only with elog call, but it also fires if plpgsql 
> code called from plperl function raises exception.

Hmm, yeah, there are a whole bunch of instances of that croak("%s",
edata->message) pattern, and most of them don't have an ancestral SV
to work from.  So we'll need a more general solution anyway, and it
may not be worth changing do_util_elog() to work in a fundamentally
different way than the rest.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-27 14:11  Tim Bunce <Tim.Bunce@pobox.com>
  parent: Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 1 reply; 15+ messages in thread

From: Tim Bunce @ 2015-09-27 14:11 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; lei@aswsyst.cz, pgsql-bugs@postgresql.org, Alex Hunsaker <badalex@gmail.com>; Tim Bunce <Tim.Bunce@pobox.com>

On Fri, Sep 25, 2015 at 04:51:20PM -0300, Alvaro Herrera wrote:
> Tom Lane wrote:
> 
> > It's still beyond my Perl-fu ...
> 
> Maybe Alex or Tim can shed some light?  Please see:
> http://www.postgresql.org/message-id/flat/20150925112345.26913.28407@wrigleys.postgresql.org

I think you want to call croak_sv(SV) with an SV containing the error
message and flagged as UTF-8.

Yim.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-27 23:46  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tim Bunce <Tim.Bunce@pobox.com>
  0 siblings, 1 reply; 15+ messages in thread

From: Tom Lane @ 2015-09-27 23:46 UTC (permalink / raw)
  To: Tim Bunce <Tim.Bunce@pobox.com>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; lei@aswsyst.cz, pgsql-bugs@postgresql.org, Alex Hunsaker <badalex@gmail.com>

Tim Bunce <Tim.Bunce@pobox.com> writes:
> I think you want to call croak_sv(SV) with an SV containing the error
> message and flagged as UTF-8.

I found croak_sv() in the headers for Perl 5.18 (present on OS X Yosemite)
but not in Perl 5.10 (present on RHEL6), much less 5.8 which is the oldest
Perl version we claim to support.  This doesn't look like a workable
answer unless we want to move the compatibility goalposts a long way.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-28 00:20  Alex Hunsaker <badalex@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 15+ messages in thread

From: Alex Hunsaker @ 2015-09-28 00:20 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Tim Bunce <Tim.Bunce@pobox.com>; Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs

On Sun, Sep 27, 2015 at 5:46 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Tim Bunce <Tim.Bunce@pobox.com> writes:
> > I think you want to call croak_sv(SV) with an SV containing the error
> > message and flagged as UTF-8.
>
> I found croak_sv() in the headers for Perl 5.18 (present on OS X Yosemite)
> but not in Perl 5.10 (present on RHEL6), much less 5.8 which is the oldest
> Perl version we claim to support.  This doesn't look like a workable
> answer unless we want to move the compatibility goalposts a long way.
>

Yeah, I was just looking at that. I couldn't find a good way to support
5.8. Doing:

const char *utf8_str = utf_e2u(str);
SV *sv = get_sv("@", TRUE);
sv_setpvf(sv, "%s", utf8_str);
SvUTF8_on(sv);
croak(NULL);

Works, but screws up the error line numbers. For example, given the below
test in plperl.out:

--
-- Test with a bad type
--
CREATE OR REPLACE FUNCTION perl_spi_prepared_bad(double precision) RETURNS
double precision AS $$
  my $x = spi_prepare('SELECT 10.0 * $1 AS a', 'does_not_exist');
  my $q = spi_query_prepared($x,$_[0]);
  my $result;
  while (defined (my $y = spi_fetchrow($q))) {
      $result = $y->{a};
  }
  spi_freeplan($x);
  return $result;
$$ LANGUAGE plperl;

Instead of:
ERROR:  type "does_not_exist" does not exist at line 2.

We get:
ERROR:  type "does_not_exist" does not exist at -e line 56.

I poked at this for a bit and didn't see a way around that.

Attached is a draft patch that at least fixes the issue for 5.10 and up. I
guess we don't want to commit the regression test unless we want to add 2
new plperl_elog.out files (one for 5.8 and one for the already existing alt
file).

Another thing to note is sv2cstr() can elog(ERROR) if the message has an
invalid byte sequence:

create or replace function perl_test_err() returns text
language plperl as $$
  elog(ERROR, "\0");
$$;

-- or
create or replace function perl_test_err() returns text
language plperl as $$
  elog(INFO, "\0");
$$;
select perl_test_err();
ERROR:  invalid byte sequence for encoding "UTF8": 0x00k

Without the PG_TRY block we can't punt these errors back to perl. Maybe
that does not matter all that much. After-all we could still have an error
converting that new error to utf8 for perl (realistically this probably
falls into the impossible category...). Thoughts?

Attached is a patch that does /not/ take into account errors thrown by
pg_any_to_server() along the lines of Tom's above suggestion. I added a
croak_cstr() function (trying to be in the spirit of the other
plperl_helper functions) and converted various croak("%s", e->message);
over to it. Tested on 5.8.9 and 5.18.2 from 9.1-stable to 9.4
(9.5 doesn't seem to pass a standard make check on my machine atm, but I
don't think there have been any plperl changes).

Note it does not apply cleaning to 9.1-9.3 (trivial rejects), I'm happy to
do the leg work if we like this proposed patch.

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

plperl_elog_encoding.patch (application/octet-stream, 4.8K) download 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..45924f1 100644 --- a/src/pl/plperl/Util.xs +++ b/src/pl/plperl/Util.xs @@ -36,31 +36,18 @@ static void do_util_elog(int level, SV *msg) { - MemoryContext oldcontext = CurrentMemoryContext; char * volatile cmsg = NULL; - PG_TRY(); + if (level < ERROR) { cmsg = sv2cstr(msg); elog(level, "%s", cmsg); pfree(cmsg); - } - PG_CATCH(); + }else { - ErrorData *edata; - - /* Must reset elog.c's state */ - MemoryContextSwitchTo(oldcontext); - edata = CopyErrorData(); - FlushErrorState(); - - if (cmsg) - pfree(cmsg); - /* Punt the error to Perl */ - croak("%s", edata->message); + croak_cstr(SvPV_nolen(msg)); } - PG_END_TRY(); } static text * diff --git a/src/pl/plperl/expected/plperl_elog.out b/src/pl/plperl/expected/plperl_elog.out index c447fa2..99d447a 100644 --- a/src/pl/plperl/expected/plperl_elog.out +++ b/src/pl/plperl/expected/plperl_elog.out @@ -104,3 +104,10 @@ PL/Perl function "indirect_die_caller" 2 (1 row) +create or replace function perl_test_err() returns text +language plperl as $$ + elog(ERROR, "Česká chyba ěščřžýáíé"); +$$; +select perl_test_err(); +ERROR: Česká chyba ěščřžýáíé at line 2. +CONTEXT: PL/Perl function "perl_test_err" 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..febdd33 100644 --- a/src/pl/plperl/plperl_helpers.h +++ b/src/pl/plperl/plperl_helpers.h @@ -121,4 +121,38 @@ 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 + /* + * without the sv_2mortal the sv never seems to be freed + */ + + croak_sv(sv_2mortal(cstr2sv(str))); +#else + /* + * croak_sv() is not available in older perls and croak() does not play + * nicely with utf8 at all + * + * If we try: + * const char *utf8_str = utf_e2u(str); + * SV *sv = get_sv("@", TRUE); + * sv_setpvf(sv, "%s", utf8_str); + * SvUTF8_on(sv); + * + * Line numbers in error messages get thrown off. + * Instead of "at line 2." we get "at -e line 58." for example. + * + * All we can do is croak() with the potentially improperly encoded string. + */ + + croak("%s", str); +#endif +} + #endif /* PL_PERL_HELPERS_H */ diff --git a/src/pl/plperl/sql/plperl_elog.sql b/src/pl/plperl/sql/plperl_elog.sql index 032fd8b..db3ae8a 100644 --- a/src/pl/plperl/sql/plperl_elog.sql +++ b/src/pl/plperl/sql/plperl_elog.sql @@ -76,3 +76,10 @@ return $a + $b; $$; select indirect_die_caller(); + +create or replace function perl_test_err() returns text +language plperl as $$ + elog(ERROR, "Česká chyba ěščřžýáíé"); +$$; + +select perl_test_err();
^ permalink raw reply [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-28 16:28  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alex Hunsaker <badalex@gmail.com>
  0 siblings, 1 reply; 15+ messages in thread

From: Tom Lane @ 2015-09-28 16:28 UTC (permalink / raw)
  To: Alex Hunsaker <badalex@gmail.com>; +Cc: Tim Bunce <Tim.Bunce@pobox.com>; Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs

Alex Hunsaker <badalex@gmail.com> writes:
> Yeah, I was just looking at that. I couldn't find a good way to support
> 5.8. Doing: ...
> Works, but screws up the error line numbers.

I looked into this and determined that the problem is that the location
info doesn't get appended to ERRSV until after Perl has popped the stack.

A possibly-entirely-wrong hack that seems to fix that is to use mess()
to construct the SV, *and* append the location info, before we croak.
I tried this implementation of croak_cstr:

	char	   *utf8_str = utf_e2u(str);
	SV *ssv = mess("%s", utf8_str);
	SV *errsv = get_sv("@", GV_ADD);

	SvUTF8_on(ssv);
	pfree(utf8_str);

	sv_setsv(errsv, ssv);
	croak(NULL);

with Perl 5.8.9 and it passed your proposed regression test.  Obvious
questions include:

* I don't see mess() in the perlapi man page, so is it okay to use?

* Is there any leakage involved in this?  (I don't know what prompted
you to add sv_2mortal() to the other implementation, but maybe we need
it here too?)

* Is there some other reason why this is wrong or a bad idea?  Since we'd
only use this with versions lacking croak_sv(), future-proofing doesn't
seem like a good argument against it, but maybe there is another one.


BTW, I think we can't actually use this regression test case, because it
won't work as-is in non-UTF8 encodings.  But the use of UTF8 characters in
the string doesn't seem like an essential property of the test.  However,
other test cases may already provide sufficient coverage if we're not
going to test encoding conversion.

Also, I'm inclined to leave do_util_elog() alone other than replacing
croak("%s", edata->message) with croak_cstr(edata->message).  Since
we have to have croak_cstr() anyway, there seems little value in taking
any risk that we've missed some reason why the existing coding there
is important.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-29 03:51  Alex Hunsaker <badalex@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 3 replies; 15+ messages in thread

From: Alex Hunsaker @ 2015-09-29 03:51 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Tim Bunce <Tim.Bunce@pobox.com>; Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs

On Mon, Sep 28, 2015 at 10:28 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Alex Hunsaker <badalex@gmail.com> writes:
> > Yeah, I was just looking at that. I couldn't find a good way to support
> > 5.8. Doing: ...
> > Works, but screws up the error line numbers.
>
> I looked into this and determined that the problem is that the location
> info doesn't get appended to ERRSV until after Perl has popped the stack.
>
>

> I tried this implementation of croak_cstr: ...


Yeah that seems to work great!



> * I don't see mess() in the perlapi man page, so is it okay to use?
>


Looks like they added it to perlapi in 5.12, given that it exists and seems
to work in 5.8 and 5.10 I think we are ok to use it. I tested your above
usage on 5.8, 5.10, 5.12.


* Is there any leakage involved in this?  (I don't know what prompted
> you to add sv_2mortal() to the other implementation, but maybe we need
> it here too?)
>

No, $@ is global and mess() also uses a global. (unless perl is
destructing).

In the croak_sv() case, we never use/assign what cstr2sv() returns anywhere
in perl land, so it always has a refcount of 1. We have similar usage of
sv_2mortal(cstr2sv()) albeit not on the same line in plperl.c.


* Is there some other reason why this is wrong or a bad idea?  Since we'd
> only use this with versions lacking croak_sv(), future-proofing doesn't
> seem like a good argument against it, but maybe there is another one.


The only argument that comes to mind is AFAIK perl 5.8 and 5.10 are more or
less unmaintained so it might not be worth the effort to make them work.
And it's been this way forever.


BTW, I think we can't actually use this regression test case, [...]
>  However,

other test cases may already provide sufficient coverage if we're not
> going to test encoding conversion.
>

Agreed. I'll attach it separately just for easy verification that
everything is working as intended.



> Also, I'm inclined to leave do_util_elog() alone other than replacing
> croak("%s", edata->message) with croak_cstr(edata->message).
>

Done that way.

The attached was tested on 5.8.9, 5.10.1, 5.12.5, 5.14.4, 5.18.2 and 5.22.0.

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments: download all (2 files, .tar.gz)

plperl_elog_enc_v2.patch (application/octet-stream, 3.2K) download 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 */
plperl_elog_enc_regress.patch (application/octet-stream, 1.5K) download diff --git a/src/pl/plperl/expected/plperl_elog.out b/src/pl/plperl/expected/plperl_elog.out index c447fa2..99d447a 100644 --- a/src/pl/plperl/expected/plperl_elog.out +++ b/src/pl/plperl/expected/plperl_elog.out @@ -104,3 +104,10 @@ PL/Perl function "indirect_die_caller" 2 (1 row) +create or replace function perl_test_err() returns text +language plperl as $$ + elog(ERROR, "Česká chyba ěščřžýáíé"); +$$; +select perl_test_err(); +ERROR: Česká chyba ěščřžýáíé at line 2. +CONTEXT: PL/Perl function "perl_test_err" diff --git a/src/pl/plperl/expected/plperl_elog_1.out b/src/pl/plperl/expected/plperl_elog_1.out index 0932fde..deaf16e 100644 --- a/src/pl/plperl/expected/plperl_elog_1.out +++ b/src/pl/plperl/expected/plperl_elog_1.out @@ -104,3 +104,10 @@ PL/Perl function "indirect_die_caller" 2 (1 row) +create or replace function perl_test_err() returns text +language plperl as $$ + elog(ERROR, "Česká chyba ěščřžýáíé"); +$$; +select perl_test_err(); +ERROR: Česká chyba ěščřžýáíé at line 2. +CONTEXT: PL/Perl function "perl_test_err" diff --git a/src/pl/plperl/sql/plperl_elog.sql b/src/pl/plperl/sql/plperl_elog.sql index 032fd8b..db3ae8a 100644 --- a/src/pl/plperl/sql/plperl_elog.sql +++ b/src/pl/plperl/sql/plperl_elog.sql @@ -76,3 +76,10 @@ return $a + $b; $$; select indirect_die_caller(); + +create or replace function perl_test_err() returns text +language plperl as $$ + elog(ERROR, "Česká chyba ěščřžýáíé"); +$$; + +select perl_test_err();
^ permalink raw reply [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-29 04:13  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alex Hunsaker <badalex@gmail.com>
  2 siblings, 0 replies; 15+ messages in thread

From: Tom Lane @ 2015-09-29 04:13 UTC (permalink / raw)
  To: Alex Hunsaker <badalex@gmail.com>; +Cc: Tim Bunce <Tim.Bunce@pobox.com>; Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs

Alex Hunsaker <badalex@gmail.com> writes:
> The attached was tested on 5.8.9, 5.10.1, 5.12.5, 5.14.4, 5.18.2 and 5.22.0.

Thanks!  Barring objections, I'll push this in the morning.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-29 14:55  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alex Hunsaker <badalex@gmail.com>
  2 siblings, 1 reply; 15+ messages in thread

From: Tom Lane @ 2015-09-29 14:55 UTC (permalink / raw)
  To: Alex Hunsaker <badalex@gmail.com>; +Cc: Tim Bunce <Tim.Bunce@pobox.com>; Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs

Alex Hunsaker <badalex@gmail.com> writes:
> On Mon, Sep 28, 2015 at 10:28 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> BTW, I think we can't actually use this regression test case, [...]

> Agreed. I'll attach it separately just for easy verification that
> everything is working as intended.

After awhile I remembered that we already had a similar test case in
plpython: it's testing with no-break space (U+A0) which has an equivalent
in most encodings.  So I adopted that strategy, and pushed this with
a test case.  We'll soon see if the buildfarm likes it.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-09-29 19:22  Alex Hunsaker <badalex@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 15+ messages in thread

From: Alex Hunsaker @ 2015-09-29 19:22 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Tim Bunce <Tim.Bunce@pobox.com>; Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs

On Tue, Sep 29, 2015 at 8:55 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Alex Hunsaker <badalex@gmail.com> writes:
> > On Mon, Sep 28, 2015 at 10:28 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >> BTW, I think we can't actually use this regression test case, [...]
>
> After awhile I remembered that we already had a similar test case in
> plpython: it's testing with no-break space (U+A0) which has an equivalent
> in most encodings.  So I adopted that strategy, and pushed this with
> a test case.  We'll soon see if the buildfarm likes it.
>
>                         regards, tom lane
>

I've been keeping a loose eye on the buildfarm, everything looks good so
far.

FYI I also ended up testing 5.16.3 and 5.20.3 which means it should be
tested on every stable version of perl.

Thanks!

^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: BUG #13638: Exception texts from plperl has bad encoding
@ 2015-10-02 11:07  Michal Leinweber <lei@aswsyst.cz>
  parent: Alex Hunsaker <badalex@gmail.com>
  2 siblings, 0 replies; 15+ messages in thread

From: Michal Leinweber @ 2015-10-02 11:07 UTC (permalink / raw)
  To: Alex Hunsaker <badalex@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Tim Bunce <Tim.Bunce@pobox.com>; Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs

Hello,

just reporting that the attached patch solves my problem. Thanks.

Best Regards
     Michal Leinweber

On 2015-09-29 05:51, Alex Hunsaker wrote:

> On Mon, Sep 28, 2015 at 10:28 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 
>> Alex Hunsaker <badalex@gmail.com> writes:
>>> Yeah, I was just looking at that. I couldn't find a good way to 
>>> support
>>> 5.8. Doing: ...
>>> Works, but screws up the error line numbers.
>> 
>> I looked into this and determined that the problem is that the 
>> location
>> info doesn't get appended to ERRSV until after Perl has popped the 
>> stack.
> 
>> I tried this implementation of croak_cstr: ...
> 
> Yeah that seems to work great!
> 
>> * I don't see mess() in the perlapi man page, so is it okay to use?
> 
> Looks like they added it to perlapi in 5.12, given that it exists and 
> seems to work in 5.8 and 5.10 I think we are ok to use it. I tested 
> your above usage on 5.8, 5.10, 5.12.
> 
>> * Is there any leakage involved in this?  (I don't know what prompted
>> you to add sv_2mortal() to the other implementation, but maybe we need
>> it here too?)
> 
> No, $@ is global and mess() also uses a global. (unless perl is 
> destructing).
> 
> In the croak_sv() case, we never use/assign what cstr2sv() returns 
> anywhere in perl land, so it always has a refcount of 1. We have 
> similar usage of sv_2mortal(cstr2sv()) albeit not on the same line in 
> plperl.c.
> 
>> * Is there some other reason why this is wrong or a bad idea?  Since 
>> we'd
>> only use this with versions lacking croak_sv(), future-proofing 
>> doesn't
>> seem like a good argument against it, but maybe there is another one.
> 
> The only argument that comes to mind is AFAIK perl 5.8 and 5.10 are 
> more or less unmaintained so it might not be worth the effort to make 
> them work. And it's been this way forever.
> 
>> BTW, I think we can't actually use this regression test case, [...]  
>> However,
> 
>> other test cases may already provide sufficient coverage if we're not
>> going to test encoding conversion.
> 
> Agreed. I'll attach it separately just for easy verification that 
> everything is working as intended.
> 
>> Also, I'm inclined to leave do_util_elog() alone other than replacing
>> croak("%s", edata->message) with croak_cstr(edata->message).
> 
> Done that way.
> 
> The attached was tested on 5.8.9, 5.10.1, 5.12.5, 5.14.4, 5.18.2 and 
> 5.22.0.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 15+ messages in thread


end of thread, other threads:[~2015-10-02 11:07 UTC | newest]

Thread overview: 15+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-09-25 11:23 BUG #13638: Exception texts from plperl has bad encoding lei@aswsyst.cz
2015-09-25 19:18 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-09-25 19:33   ` Tom Lane <tgl@sss.pgh.pa.us>
2015-09-25 19:51     ` Alvaro Herrera <alvherre@2ndquadrant.com>
2015-09-27 14:11       ` Tim Bunce <Tim.Bunce@pobox.com>
2015-09-27 23:46         ` Tom Lane <tgl@sss.pgh.pa.us>
2015-09-28 00:20           ` Alex Hunsaker <badalex@gmail.com>
2015-09-28 16:28             ` Tom Lane <tgl@sss.pgh.pa.us>
2015-09-29 03:51               ` Alex Hunsaker <badalex@gmail.com>
2015-09-29 04:13                 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-09-29 14:55                 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-09-29 19:22                   ` Alex Hunsaker <badalex@gmail.com>
2015-10-02 11:07                 ` Michal Leinweber <lei@aswsyst.cz>
2015-09-25 20:15     ` Michal Leinweber <lei@aswsyst.cz>
2015-09-25 20:44       ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox