Fix PL/Perl to cope with errors thrown from tied structures. An error thrown from a tied array, hash, or scalar may occur while PL/Perl is trying to return a function value, in which case we are outside the call_sv() execution loop that would normally handle trapping of Perl errors. That is problematic: if Perl doesn't think it is inside an eval-like context, it will bleat on stderr and then exit(); while if it does think that, it will longjmp out to some outer level of Perl execution, which wreaks havoc. We need to keep Perl's jmp_buf stack in sync with Postgres', so that we never have a situation where one of those stack pointers points above the function level the other stack was just used to jump out to.
The core idea of this fix is to replace PG_TRY blocks within PL/Perl with "PG_PERL_TRY" blocks, which have fundamentally the same semantics as PG_TRY, but establish new jmp_buf levels within both stacks and pop both correctly at exit. If a Perl error is caught using Perl's stack, we'll convert it to a Postgres error using logic similar to what PL/Perl uses for the purpose elsewhere. This simple core idea has some complications though. One big one is that since PG_PERL_TRY adjusts the state of the current Perl interpreter, we must make sure that we don't switch interpreters partway through the block. Which the old code did freely. To fix that without adding more TRY blocks than necessary, refactor the contract between plperl_call_handler and its sub-handlers (plperl_func_handler et al) so that we look up the target function and switch to the associated interpreter before we enter the sub-handler. I also spent a deal of effort on tightening the error-condition guarantees for select_perl_context and plperl_init_interp, as per the changes to their header comments. As they stood, it was really hard to say anything about what state would be left behind after a failure during Perl interpreter creation, which cascaded into not being able to promise anything about what would happen in their callers. Within PG_PERL_TRY, there are more problems. Aside from just pushing a new jmp_buf stack level (handled by JMPENV_PUSH/JMPENV_POP), we must see to it that if control does reach Perl_die then it will choose to longjmp() rather than summarily exit(). At least in Perl 5.40, there are two gating tests: PL_in_eval must be nonzero (preferably with the EVAL_INEVAL bit set), and S_dopoptoeval must find a CXt_EVAL context on the PERL_CONTEXT stack. We could hack PL_in_eval directly, but there's no direct way to manipulate the context stack. The solution used here is to execute OP_ENTERTRY and then (if a longjmp didn't happen) OP_LEAVETRY. That requires manually saving/restoring PL_op, as well as ERRSV, but otherwise seems to work fairly well. Another interesting bit is that if possible, we create only one jmp_buf struct and link it into both error stacks. It'd be cleaner to keep the two mechanisms entirely separate, but that would require two sigsetjmp calls in close proximity, which turns out to break clang's optimizer quite thoroughly. We do get the small benefit of saving the cycles that would go into a second sigsetjmp call. A complication is that we need to be able to figure out whether a caught longjmp was triggered by Perl or Postgres. We do that by adding some state in elog.c so that we can tell whether the topmost error data is complete or not. If it isn't, then the error was thrown by Perl. (An example where this distinction matters is that the sv2cstr calls used in plperl.c's own ereports could throw Perl errors, at a point where we are still filling in a Postgres error.) Ideally this state would be carried by a new flag in ErrorData, but to avoid an ABI break in stable branches, hide it inside elog.c for now. However, we can't use that trick on MinGW-64, because there we use gcc's __builtin_setjmp() while Perl uses MinGW's plain setjmp(). So in that case, resort to managing the jmp_buf stacks separately. Reported-by: Ngoc Trần <[email protected]> Reported-by: Anthropic OSS program Author: Tom Lane <[email protected]> Backpatch-through: 14 Security: CVE-2026-116326 Branch ------ REL_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/c6d81df553d95c0a1bc39f94ac90c9d73944b1d2 Author: Tom Lane <[email protected]> Modified Files -------------- src/backend/utils/error/elog.c | 22 ++ src/include/c.h | 1 + src/include/utils/elog.h | 4 + src/pl/plperl/expected/plperl_tied.out | 43 ++++ src/pl/plperl/plperl.c | 399 +++++++++++++++++++-------------- src/pl/plperl/plperl.h | 283 +++++++++++++++++++++++ src/pl/plperl/sql/plperl_tied.sql | 31 +++ 7 files changed, 615 insertions(+), 168 deletions(-)
