Make plperl's handling of Perl arrays safer and more consistent. plperl_func_handler()'s stanza for handling an arrayref result in a SETOF function could loop forever (or at least till OOM) when given a tied array, since av_fetch won't necessarily ever return a null pointer in that case. Be consistent with the other places where we traverse a perl array: call av_len() once and use len+1 as the loop limit, silently ignoring any null pointers we get back from that range of subscripts.
But actually, Perl's preferred locution for this seems to be to use av_count() not av_len()+1. av_count() seems better since there's less risk of forgetting to add 1. Also, both of those functions return Size_t (or SSize_t) not int, creating at least a theoretical overflow hazard. While we're modernizing this, let's use the correct variable type where we can, and include an overflow check where we can't. Reported-by: Claude Code (via Noah Misch) Author: Tom Lane <[email protected]> Reviewed-by: Andrey Rachitskiy <[email protected]> Discussion: https://postgr.es/m/[email protected] Backpatch-through: 14 Branch ------ REL_14_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/ede3188e118f81ea933ac5d64866190024f678a7 Modified Files -------------- contrib/jsonb_plperl/jsonb_plperl.c | 5 ++--- src/pl/plperl/plperl.c | 41 ++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 13 deletions(-)
