postgres.git / summary / log / commit / refs

commit    f1b267e784135c74cfd4144902fd41ceb1cda0a4
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Tue Nov 10 21:32:37 2026 +0000
First released in: 19.0

    Fix missing error handling in plperl_sv_to_literal().
    
    This function should have logic to trap Postgres errors and
    convert them to Perl croak(), but it was missing that.
    We'd somehow managed not to notice, even though the case of
    an error was tested, until commits daafcbd11 et al added more
    complexity to the interaction of Postgres and Perl errors.
    Even then it mostly worked, but a few PPC64 buildfarm animals
    are showing failures that trace to this.  Alexander Lakhin
    then found that the crash is repeatable on other platforms
    if you repeat the test a few times.
    
    This patch basically just copies-and-pastes the error handling
    from plperl_spi_exec() into plperl_sv_to_literal().


src/pl/plperl/expected/plperl_util.out | 12 ++++- src/pl/plperl/plperl.c | 88 ++++++++++++++++++++++++++-------- src/pl/plperl/sql/plperl_util.sql | 4 ++ 3 files changed, 83 insertions(+), 21 deletions(-) diff --git a/src/pl/plperl/expected/plperl_util.out b/src/pl/plperl/expected/plperl_util.out index 698a8a17fe7..17b622e03fd 100644 --- a/src/pl/plperl/expected/plperl_util.out +++ b/src/pl/plperl/expected/plperl_util.out @@ -194,5 +194,15 @@ create or replace function perl_encode_typed_literal() returns setof text langua return_next encode_typed_literal({a => 0, b => ['PL','/','Perl']}, 'perl_foo_pos'); $$; select perl_encode_typed_literal(); -- fail -ERROR: value for domain perl_foo_pos violates check constraint "perl_foo_pos_check" +ERROR: value for domain perl_foo_pos violates check constraint "perl_foo_pos_check" at line 2. +CONTEXT: PL/Perl function "perl_encode_typed_literal" +-- on some platforms, this crashed after several iterations +select perl_encode_typed_literal(); -- fail +ERROR: value for domain perl_foo_pos violates check constraint "perl_foo_pos_check" at line 2. +CONTEXT: PL/Perl function "perl_encode_typed_literal" +select perl_encode_typed_literal(); -- fail +ERROR: value for domain perl_foo_pos violates check constraint "perl_foo_pos_check" at line 2. +CONTEXT: PL/Perl function "perl_encode_typed_literal" +select perl_encode_typed_literal(); -- fail +ERROR: value for domain perl_foo_pos violates check constraint "perl_foo_pos_check" at line 2. CONTEXT: PL/Perl function "perl_encode_typed_literal" diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index 724673c9021..22c983579b3 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -1600,36 +1600,84 @@ plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, } } -/* Convert the perl SV to a string returned by the type output function */ +/* + * Convert the perl SV to a string returned by the type output function, + * or NULL if the SV is undef. + */ char * plperl_sv_to_literal(SV *sv, char *fqtypename) { - Oid typid; - Oid typoutput; - Datum datum; - bool typisvarlena, - isnull; + /* + * Execute the conversion inside a sub-transaction, so we can cope with + * errors sanely + */ + dTHX; + char *volatile result = NULL; + MemoryContext oldcontext = CurrentMemoryContext; + ResourceOwner oldowner = CurrentResourceOwner; check_spi_usage_allowed(); - typid = DirectFunctionCall1(regtypein, CStringGetDatum(fqtypename)); - if (!OidIsValid(typid)) - ereport(ERROR, - (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("lookup failed for type %s", fqtypename))); + BeginInternalSubTransaction(NULL); + /* Want to run inside function's memory context */ + MemoryContextSwitchTo(oldcontext); + + PG_PERL_TRY(); + { + Oid typid; + Oid typoutput; + Datum datum; + bool typisvarlena, + isnull; - datum = plperl_sv_to_datum(sv, - typid, -1, - NULL, NULL, InvalidOid, - &isnull); + typid = DatumGetObjectId(DirectFunctionCall1(regtypein, + CStringGetDatum(fqtypename))); + if (!OidIsValid(typid)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("lookup failed for type %s", fqtypename))); - if (isnull) - return NULL; + datum = plperl_sv_to_datum(sv, + typid, -1, + NULL, NULL, InvalidOid, + &isnull); + + if (!isnull) + { + getTypeOutputInfo(typid, + &typoutput, &typisvarlena); + + result = OidOutputFunctionCall(typoutput, datum); + } + + /* Commit the inner transaction, return to outer xact context */ + ReleaseCurrentSubTransaction(); + MemoryContextSwitchTo(oldcontext); + CurrentResourceOwner = oldowner; + } + PG_PERL_CATCH(); + { + ErrorData *edata; + + /* Save error info */ + MemoryContextSwitchTo(oldcontext); + edata = CopyErrorData(); + FlushErrorState(); + + /* Abort the inner transaction */ + RollbackAndReleaseCurrentSubTransaction(); + MemoryContextSwitchTo(oldcontext); + CurrentResourceOwner = oldowner; + + /* Punt the error to Perl */ + croak_cstr(edata->message); - getTypeOutputInfo(typid, - &typoutput, &typisvarlena); + /* Can't get here, but keep compiler quiet */ + return NULL; + } + PG_PERL_END_TRY(); - return OidOutputFunctionCall(typoutput, datum); + return result; } /* diff --git a/src/pl/plperl/sql/plperl_util.sql b/src/pl/plperl/sql/plperl_util.sql index 5b31605ccde..3b47a34c295 100644 --- a/src/pl/plperl/sql/plperl_util.sql +++ b/src/pl/plperl/sql/plperl_util.sql @@ -119,3 +119,7 @@ create or replace function perl_encode_typed_literal() returns setof text langua $$; select perl_encode_typed_literal(); -- fail +-- on some platforms, this crashed after several iterations +select perl_encode_typed_literal(); -- fail +select perl_encode_typed_literal(); -- fail +select perl_encode_typed_literal(); -- fail [parent: f5c41f6681c9]