From 8e46e9e036166a687c479a0e65756e960c5d7be0 Mon Sep 17 00:00:00 2001
From: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Date: Mon, 16 Nov 2026 23:57:23 +0500
Subject: [PATCH] Fix unchecked NULL returns in PL/Python type conversion

PLyDict_FromTuple() passed a NULL from att->func() to
PyDict_SetItemString(), crashing the backend.
PLyList_FromArray_recurse() passed the same NULL to PyList_SetItem().
PLyObject_AsString() passed a NULL from PyObject_Repr() or PyObject_Str()
to PLyUnicode_Bytes().

Dict and list insertion goes through PLy_dict_setitem_string() and
PLy_list_setitem(). Other NULL object pointers go through PLy_check(),
which reports the Python error with PLy_elog().

A missing mapping key or object attribute is still reported as before.
Any other exception from those lookups is reported with PLy_elog().

BUG #19899
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reported-by: Marcos Castedo <marcos.castedo@anachronics.com>
Discussion: https://www.postgresql.org/message-id/19899-3e21845d988f0c0f@postgresql.org
---
 src/pl/plpython/plpy_typeio.c | 50 +++++++++++++++++------------
 src/pl/plpython/plpy_util.c   | 60 +++++++++++++++++++++++++++++++++++
 src/pl/plpython/plpy_util.h   |  6 ++++
 3 files changed, 96 insertions(+), 20 deletions(-)

diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c
index e499ed8cbfd..7975836766c 100644
--- a/src/pl/plpython/plpy_typeio.c
+++ b/src/pl/plpython/plpy_typeio.c
@@ -104,7 +104,7 @@ PLy_input_convert(PLyDatumToOb *arg, Datum val)
 
 	MemoryContextSwitchTo(oldcontext);
 
-	return result;
+	return PLy_check(result);
 }
 
 /*
@@ -149,7 +149,7 @@ PLy_input_from_tuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool in
 
 	MemoryContextSwitchTo(oldcontext);
 
-	return dict;
+	return PLy_check(dict);
 }
 
 /*
@@ -723,7 +723,7 @@ 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);
-			PyList_SetItem(list, i, sublist);
+			PLy_list_setitem(list, i, sublist);
 		}
 	}
 	else
@@ -741,16 +741,13 @@ PLyList_FromArray_recurse(PLyDatumToOb *elm, int *dims, int ndim, int dim,
 		{
 			/* checking for NULL */
 			if (bitmap && (*bitmap & bitmask) == 0)
-			{
-				Py_INCREF(Py_None);
-				PyList_SetItem(list, i, Py_None);
-			}
+				PLy_list_setitem(list, i, Py_None);
 			else
 			{
 				Datum		itemvalue;
 
 				itemvalue = fetch_att(dataptr, elm->typbyval, elm->typlen);
-				PyList_SetItem(list, i, elm->func(elm, itemvalue));
+				PLy_list_setitem(list, i, elm->func(elm, itemvalue));
 				dataptr = att_addlength_pointer(dataptr, elm->typlen, dataptr);
 				dataptr = (char *) att_nominal_alignby(dataptr, typalignby);
 			}
@@ -835,7 +832,6 @@ PLyDict_FromTuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool inclu
 			char	   *key;
 			Datum		vattr;
 			bool		is_null;
-			PyObject   *value;
 
 			if (attr->attisdropped)
 				continue;
@@ -854,13 +850,9 @@ 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);
+				PLy_dict_setitem_string(dict, key, Py_None);
 			else
-			{
-				value = att->func(att, vattr);
-				PyDict_SetItemString(dict, key, value);
-				Py_DECREF(value);
-			}
+				PLy_dict_setitem_string(dict, key, att->func(att, vattr));
 		}
 	}
 	PG_CATCH();
@@ -1037,22 +1029,25 @@ PLyObject_AsString(PyObject *plrv)
 	else if (PyFloat_Check(plrv))
 	{
 		/* use repr() for floats, str() is lossy */
-		PyObject   *s = PyObject_Repr(plrv);
+		PyObject   *s = PLy_check(PyObject_Repr(plrv));
 
 		plrv_bo = PLyUnicode_Bytes(s);
-		Py_XDECREF(s);
+		Py_DECREF(s);
 	}
 	else
 	{
-		PyObject   *s = PyObject_Str(plrv);
+		PyObject   *s = PLy_check(PyObject_Str(plrv));
 
 		plrv_bo = PLyUnicode_Bytes(s);
-		Py_XDECREF(s);
+		Py_DECREF(s);
 	}
 	if (!plrv_bo)
 		PLy_elog(ERROR, "could not create string representation of Python object");
 
-	plrv_sc = pstrdup(PyBytes_AsString(plrv_bo));
+	plrv_sc = PyBytes_AsString(plrv_bo);
+	if (plrv_sc == NULL)
+		PLy_elog(ERROR, "could not create string representation of Python object");
+	plrv_sc = pstrdup(plrv_sc);
 	plen = PyBytes_Size(plrv_bo);
 	slen = strlen(plrv_sc);
 
@@ -1381,11 +1376,20 @@ PLyMapping_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *mapping)
 		{
 			value = PyMapping_GetItemString(mapping, key);
 			if (!value)
+			{
+				/*
+				 * A missing key sets KeyError. Another exception, such as
+				 * MemoryError, is a real failure.
+				 */
+				if (!PyErr_ExceptionMatches(PyExc_KeyError))
+					PLy_elog(ERROR, NULL);
+				PyErr_Clear();
 				ereport(ERROR,
 						(errcode(ERRCODE_UNDEFINED_COLUMN),
 						 errmsg("key \"%s\" not found in mapping", key),
 						 errhint("To return null in a column, "
 								 "add the value None to the mapping with the key named after the column.")));
+			}
 
 			values[i] = att->func(att, value, &nulls[i], false);
 
@@ -1535,7 +1539,13 @@ PLyGenericObject_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *object
 				 * it's interpreted as a two-dimensional array, and we try to
 				 * interpret "123" as the composite type. See also similar
 				 * heuristic in PLyObject_ToScalar().
+				 *
+				 * A missing attribute sets AttributeError. Another exception,
+				 * such as MemoryError, is a real failure.
 				 */
+				if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+					PLy_elog(ERROR, NULL);
+				PyErr_Clear();
 				ereport(ERROR,
 						(errcode(ERRCODE_UNDEFINED_COLUMN),
 						 errmsg("attribute \"%s\" does not exist in Python object", key),
diff --git a/src/pl/plpython/plpy_util.c b/src/pl/plpython/plpy_util.c
index c2338decba2..bc7eb4515f2 100644
--- a/src/pl/plpython/plpy_util.c
+++ b/src/pl/plpython/plpy_util.c
@@ -117,3 +117,63 @@ PLyUnicode_FromString(const char *s)
 {
 	return PLyUnicode_FromStringAndSize(s, strlen(s));
 }
+
+/*
+ * Return obj. NULL means a Python exception is set. Report it with
+ * PLy_elog(ERROR) and do not return.
+ */
+PyObject *
+PLy_check(PyObject *obj)
+{
+	if (obj == NULL)
+		PLy_elog(ERROR, NULL);
+	return obj;
+}
+
+/*
+ * Insert value into dict under key.
+ *
+ * value is a new reference owned by the caller, or NULL with a Python
+ * exception set. A new reference is stolen. Py_None is borrowed, and this
+ * function takes its own reference. On failure the Python exception is
+ * reported with PLy_elog(ERROR).
+ */
+void
+PLy_dict_setitem_string(PyObject *dict, const char *key, PyObject *value)
+{
+	if (value == NULL)
+		PLy_elog(ERROR, NULL);
+
+	if (value == Py_None)
+		Py_INCREF(Py_None);
+
+	if (PyDict_SetItemString(dict, key, value) < 0)
+	{
+		Py_DECREF(value);
+		PLy_elog(ERROR, NULL);
+	}
+	Py_DECREF(value);
+}
+
+/*
+ * Store value at index i.
+ *
+ * value is a new reference owned by the caller, or NULL with a Python
+ * exception set. A new reference is stolen. Py_None is borrowed, and this
+ * function takes its own reference. listobject.h says PyList_SetItem
+ * decrements the new reference even when the item is not inserted, so it
+ * is not released again here. On failure the Python exception is reported
+ * with PLy_elog(ERROR).
+ */
+void
+PLy_list_setitem(PyObject *list, Py_ssize_t i, PyObject *value)
+{
+	if (value == NULL)
+		PLy_elog(ERROR, NULL);
+
+	if (value == Py_None)
+		Py_INCREF(Py_None);
+
+	if (PyList_SetItem(list, i, value) < 0)
+		PLy_elog(ERROR, NULL);
+}
diff --git a/src/pl/plpython/plpy_util.h b/src/pl/plpython/plpy_util.h
index 6f491b0f95b..2c3a7d4c06c 100644
--- a/src/pl/plpython/plpy_util.h
+++ b/src/pl/plpython/plpy_util.h
@@ -14,4 +14,10 @@ extern PGDLLEXPORT char *PLyUnicode_AsString(PyObject *unicode);
 extern PGDLLEXPORT PyObject *PLyUnicode_FromString(const char *s);
 extern PGDLLEXPORT PyObject *PLyUnicode_FromStringAndSize(const char *s, Py_ssize_t size);
 
+extern PGDLLEXPORT PyObject *PLy_check(PyObject *obj);
+extern PGDLLEXPORT void PLy_dict_setitem_string(PyObject *dict, const char *key,
+												PyObject *value);
+extern PGDLLEXPORT void PLy_list_setitem(PyObject *list, Py_ssize_t i,
+										 PyObject *value);
+
 #endif							/* PLPY_UTIL_H */
-- 
2.53.0

