plpython: NULL pointer dereference on broken sequence objects
Richard Guo <guofenglinux@gmail.com>
From: Richard Guo <guofenglinux@gmail.com>
To: Pg Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2026-06-25T08:49:31Z
Lists: pgsql-hackers
Attachments
- v1-0001-plpython-Fix-NULL-pointer-dereference-for-broken-.patch (application/octet-stream) patch v1-0001
While looking into the recent plperl NULL pointer dereference issue,
which ended up as 4015abe14, I found a similar issue in plpython, with
the help of an LLM tool (Claude 4.8).
There are 6 callers of PySequence_GetItem() in plpython, and none of
them checks the returned result before using it. PySequence_GetItem()
can return NULL whenever an element cannot be fetched, so an object
that claims a length it cannot actually deliver is enough to crash the
backend.
For example:
CREATE FUNCTION test() RETURNS int[] AS $$
class C:
def __len__(self):
return 2
def __getitem__(self, i):
raise ValueError('boom')
return C()
$$ LANGUAGE plpython3u;
SELECT test(); -- crashes
The attached patch checks the result of PySequence_GetItem() in each
place and errors out if it is NULL.
- Richard
Commits
-
plpython: Fix NULL pointer dereferences for broken sequence and mapping objects
- 0b7719f744e6 14 (unreleased) landed
- 12bff46ff3eb 15 (unreleased) landed
- e92f23bde60e 16 (unreleased) landed
- 3dc59c1737d2 17 (unreleased) landed
- 53482fcb94a8 18 (unreleased) landed
- 8612f0b7ce09 19 (unreleased) landed