From 9811257aad65e17d2115d234fe15e0f10e6eaedf Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Mon, 16 Nov 2026 14:35:47 -0500 Subject: [PATCH v2 1/2] Fix PL/Python crashes when a value can't be converted to Python PL/Python's input conversion functions follow the Python convention of returning NULL, with a Python exception set, when an object can't be built, as when copying a large value runs out of memory. None of their callers checked for that. Building the result rows of plpy.execute(), a row-type argument, or a trigger's TD["new"] or TD["old"] passed the NULL to PyDict_SetItemString() and crashed the backend, and arrays stored it as an element. A scalar argument was worse: the NULL was taken for an SQL null, so the function silently ran with None. Throw an error in PLy_input_convert() and PLy_input_from_tuple() instead, and pass None to a function only for an actual SQL null. Reported-by: Marcos Castedo Discussion: https://postgr.es/m/19899-3e21845d988f0c0f@postgresql.org Backpatch-through: 14 --- src/pl/plpython/plpy_exec.c | 7 ++----- src/pl/plpython/plpy_typeio.c | 37 ++++++++++++++++++++++++++++++++--- src/pl/plpython/plpy_typeio.h | 5 ++++- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c index 14e63625070..0b5f215c713 100644 --- a/src/pl/plpython/plpy_exec.c +++ b/src/pl/plpython/plpy_exec.c @@ -563,15 +563,12 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc) PLyDatumToOb *arginfo = &proc->args[i]; if (fcinfo->args[i].isnull) - arg = NULL; - else - arg = PLy_input_convert(arginfo, fcinfo->args[i].value); - - if (arg == NULL) { Py_INCREF(Py_None); arg = Py_None; } + else + arg = PLy_input_convert(arginfo, fcinfo->args[i].value); if (PyList_SetItem(args, i, arg) == -1) PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments"); diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c index e499ed8cbfd..2fc997dfea6 100644 --- a/src/pl/plpython/plpy_typeio.c +++ b/src/pl/plpython/plpy_typeio.c @@ -76,6 +76,10 @@ static Datum PLyGenericObject_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyO * * This is the outer-level entry point for any input conversion. Internally, * the conversion functions recurse directly to each other. + * + * The individual conversion functions follow the Python convention of + * returning NULL, with a Python exception set, on failure. Our callers + * aren't prepared for that, so we turn it into a PostgreSQL error here. */ PyObject * PLy_input_convert(PLyDatumToOb *arg, Datum val) @@ -104,6 +108,9 @@ PLy_input_convert(PLyDatumToOb *arg, Datum val) MemoryContextSwitchTo(oldcontext); + if (result == NULL) + PLy_elog(ERROR, "could not convert value to a Python object"); + return result; } @@ -149,6 +156,9 @@ PLy_input_from_tuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool in MemoryContextSwitchTo(oldcontext); + if (dict == NULL) + PLy_elog(ERROR, "could not convert row to a Python dictionary"); + return dict; } @@ -723,6 +733,11 @@ PLyList_FromArray_recurse(PLyDatumToOb *elm, int *dims, int ndim, int dim, sublist = PLyList_FromArray_recurse(elm, dims, ndim, dim + 1, dataptr_p, bitmap_p, bitmask_p); + if (sublist == NULL) + { + Py_DECREF(list); + return NULL; + } PyList_SetItem(list, i, sublist); } } @@ -748,9 +763,16 @@ PLyList_FromArray_recurse(PLyDatumToOb *elm, int *dims, int ndim, int dim, else { Datum itemvalue; + PyObject *item; itemvalue = fetch_att(dataptr, elm->typbyval, elm->typlen); - PyList_SetItem(list, i, elm->func(elm, itemvalue)); + item = elm->func(elm, itemvalue); + if (item == NULL) + { + Py_DECREF(list); + return NULL; + } + PyList_SetItem(list, i, item); dataptr = att_addlength_pointer(dataptr, elm->typlen, dataptr); dataptr = (char *) att_nominal_alignby(dataptr, typalignby); } @@ -854,12 +876,21 @@ PLyDict_FromTuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool inclu vattr = heap_getattr(tuple, (i + 1), desc, &is_null); if (is_null) - PyDict_SetItemString(dict, key, Py_None); + { + if (PyDict_SetItemString(dict, key, Py_None) < 0) + PLy_elog(ERROR, "could not add column \"%s\" to Python dictionary", key); + } else { + int rc; + value = att->func(att, vattr); - PyDict_SetItemString(dict, key, value); + if (value == NULL) + PLy_elog(ERROR, "could not convert column \"%s\" to a Python object", key); + rc = PyDict_SetItemString(dict, key, value); Py_DECREF(value); + if (rc < 0) + PLy_elog(ERROR, "could not add column \"%s\" to Python dictionary", key); } } } diff --git a/src/pl/plpython/plpy_typeio.h b/src/pl/plpython/plpy_typeio.h index 29258509b5c..d9d4b79875b 100644 --- a/src/pl/plpython/plpy_typeio.h +++ b/src/pl/plpython/plpy_typeio.h @@ -17,7 +17,10 @@ struct PLyProcedure; /* avoid requiring plpy_procedure.h here */ * "Input" conversion from PostgreSQL Datum to a Python object. * * arg is the previously-set-up conversion data, val is the value to convert. - * val mustn't be NULL. + * val mustn't be NULL. The result is a new reference, or NULL with a Python + * exception set if Python fails to construct the object. Conversion can also + * throw a PostgreSQL error. PLy_input_convert and PLy_input_from_tuple never + * return NULL. * * Note: the conversion data structs should be regarded as private to * plpy_typeio.c. We declare them here only so that other modules can -- 2.37.1 (Apple Git-137.1)