[PATCH] Fix segmentation fault and infinite loop in jsonb_{plperl,plpython}
Aleksander Alekseev <aleksander@tigerdata.com>
From: Aleksander Alekseev <aleksander@tigerdata.com>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2026-06-16T14:05:20Z
Lists: pgsql-hackers
Attachments
- v1-0002-jsonb_plperl-Replace-reference-unwinding-loop-wit.patch (text/x-patch) patch v1-0002
- v1-0001-jsonb_plperl-jsonb_plpython-Add-missing-check_sta.patch (text/x-patch) patch v1-0001
- reproduce_circular_ref.py (text/x-python)
- reproduce_stack_overflow.py (text/x-python)
Hi,
I discovered several bugs in jsonb_plperl and jsonb_plpython.
The first bug causes a segfault when dealing with deeply nested JSONB
values. As an example:
```
$ ./reproduce_stack_overflow.py
plpython3u (depth=100000): SIGSEGV
2026-06-16 16:42:56.989 MSK [3209763] LOG: client backend (PID
3209810) was terminated by signal 11: Segmentation fault
2026-06-16 16:42:56.989 MSK [3209763] DETAIL: Failed process was
running: SELECT py_deep(100000);
plperl (depth=100000): SIGSEGV
2026-06-16 16:42:59.101 MSK [3209763] LOG: client backend (PID
3209827) was terminated by signal 11: Segmentation fault
2026-06-16 16:42:59.101 MSK [3209763] DETAIL: Failed process was
running: SELECT perl_deep(100000);
```
The second bug affects only jsonb_plperl. It's possible to construct a
Perl object with circular references which will cause
SV_to_JsonbValue() to go into an infinite loop here:
```
while (SvROK(in))
in = SvRV(in);
```
The attached script reproduce_circular_ref.py reproduces the issue. Be
careful if you decide to run it because the backend will become
unresponsive to pg_cancel_backend() and you will be unable to stop the
cluster in a standard way.
I suggest fixing it by rewriting the while loop into a recursion with
check_stack_depth() call. This will make the behavior consistent with
jsonb_plpython.
Patches are attached. Thoughts?
--
Best regards,
Aleksander Alekseev
Commits
-
hstore_plperl: Add CHECK_FOR_INTERRUPTS() in reference-unwinding loop.
- c0f17b04d906 19 (unreleased) landed
- d65cf6f800f0 18 (unreleased) landed
- 60a1d712a472 16 (unreleased) landed
- 4fbbabb0a729 15 (unreleased) landed
- 4efef9d18e63 17 (unreleased) landed
- 1f6b2295f952 14 (unreleased) landed
-
jsonb_plperl, jsonb_plpython: Fix unguarded recursion and loops.
- e3b7a43fa9b6 18 (unreleased) landed
- da82fbb8f9a3 19 (unreleased) landed
- b0f3465b6f6a 16 (unreleased) landed
- a6f23c3ade0e 15 (unreleased) landed
- 639fff5118a8 14 (unreleased) landed
- 3df0b77558d7 17 (unreleased) landed