From 4686e4c53ea0e3ffca54c01591c26539a350a37d Mon Sep 17 00:00:00 2001
From: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Date: Tue, 17 Nov 2026 10:31:05 +0500
Subject: [PATCH v4] Fix unchecked NULL returns in PL/Python object
 construction

PL/Python input conversion functions return NULL with a Python exception
set when the object cannot be built, most often on out of memory. Most
callers did not check.

PLyDict_FromTuple() passed that NULL to PyDict_SetItemString(), which
increments the reference count of the value and crashed the backend.
PLy_function_build_args() treated it as a SQL NULL and ran the function
with None, discarding the Python error. PLyList_FromArray_recurse()
stored it into the list, and the trigger TD dictionary ignored the result
of every insert.

Route those stores through PLyDict_SetItemString(), PLyDict_SetItemNone()
and PLyList_SetItem(). They consume the caller's reference and report the
Python exception under a message the caller supplies, so the exception
text lands in the detail. PLy_elog_impl_v() is the va_list entry point
they need. PLy_input_convert() and PLy_input_from_tuple() now throw
instead of returning NULL.

PLy_function_build_args() also left arg set after PyList_SetItem() had
consumed the reference, so the PG_CATCH block released it a second time.

PLyMapping_ToComposite() and PLyGenericObject_ToComposite() reported
every failure as a missing key or attribute. Only KeyError and
AttributeError mean that; any other exception is now reported as itself.

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/expected/plpython_record.out |  28 ++++
 src/pl/plpython/nls.mk                       |   6 +
 src/pl/plpython/plpy_elog.c                  |  22 ++-
 src/pl/plpython/plpy_elog.h                  |   2 +
 src/pl/plpython/plpy_exec.c                  | 125 ++++++++-------
 src/pl/plpython/plpy_typeio.c                | 155 ++++++++++++-------
 src/pl/plpython/plpy_typeio.h                |   4 +
 src/pl/plpython/plpy_util.c                  |  81 ++++++++++
 src/pl/plpython/plpy_util.h                  |   8 +
 src/pl/plpython/sql/plpython_record.sql      |  22 +++
 10 files changed, 343 insertions(+), 110 deletions(-)

diff --git a/src/pl/plpython/expected/plpython_record.out b/src/pl/plpython/expected/plpython_record.out
index 26eca741f77..2bfc5b370d1 100644
--- a/src/pl/plpython/expected/plpython_record.out
+++ b/src/pl/plpython/expected/plpython_record.out
@@ -399,3 +399,31 @@ ERROR:  malformed record literal: "foo"
 DETAIL:  Missing left parenthesis.
 CONTEXT:  while creating return value
 PL/Python function "test_type_record_error4"
+-- a mapping that fails for a reason other than a missing key
+CREATE FUNCTION test_type_record_error5() RETURNS type_record AS $$
+    class broken_mapping(dict):
+        def __getitem__(self, key):
+            raise TypeError('broken mapping')
+    return broken_mapping(first='first', second=1)
+$$ LANGUAGE plpython3u;
+SELECT * FROM test_type_record_error5();
+ERROR:  could not get value for key "first" from mapping
+DETAIL:  TypeError: broken mapping
+CONTEXT:  Traceback (most recent call last):
+while creating return value
+PL/Python function "test_type_record_error5"
+-- an object whose attribute lookup fails for a reason other than a
+-- missing attribute
+CREATE FUNCTION test_type_record_error6() RETURNS type_record AS $$
+    class broken_object:
+        first = 'first'
+        def __getattr__(self, name):
+            raise ValueError('broken attribute')
+    return broken_object()
+$$ LANGUAGE plpython3u;
+SELECT * FROM test_type_record_error6();
+ERROR:  could not get attribute "second" of Python object
+DETAIL:  ValueError: broken attribute
+CONTEXT:  Traceback (most recent call last):
+while creating return value
+PL/Python function "test_type_record_error6"
diff --git a/src/pl/plpython/nls.mk b/src/pl/plpython/nls.mk
index f6aafcf2494..714d24fab7b 100644
--- a/src/pl/plpython/nls.mk
+++ b/src/pl/plpython/nls.mk
@@ -13,10 +13,16 @@ GETTEXT_FILES    = plpy_cursorobject.c \
                    plpy_typeio.c \
                    plpy_util.c
 GETTEXT_TRIGGERS = $(BACKEND_COMMON_GETTEXT_TRIGGERS) \
+                   PLyDict_SetItemNone:3 \
+                   PLyDict_SetItemString:4 \
+                   PLyList_SetItem:4 \
                    PLy_elog:2 \
                    PLy_exception_set:2 \
                    PLy_exception_set_plural:2,3
 GETTEXT_FLAGS    = $(BACKEND_COMMON_GETTEXT_FLAGS) \
+                   PLyDict_SetItemNone:3:c-format \
+                   PLyDict_SetItemString:4:c-format \
+                   PLyList_SetItem:4:c-format \
                    PLy_elog:2:c-format \
                    PLy_exception_set:2:c-format \
                    PLy_exception_set_plural:2:c-format \
diff --git a/src/pl/plpython/plpy_elog.c b/src/pl/plpython/plpy_elog.c
index 7c5ddb06ea8..99e2c6ed1a8 100644
--- a/src/pl/plpython/plpy_elog.c
+++ b/src/pl/plpython/plpy_elog.c
@@ -42,6 +42,20 @@ static bool set_string_attr(PyObject *obj, char *attrname, char *str);
  */
 void
 PLy_elog_impl(int elevel, const char *fmt, ...)
+{
+	va_list		ap;
+
+	va_start(ap, fmt);
+	PLy_elog_impl_v(elevel, fmt, ap);
+	va_end(ap);
+}
+
+/*
+ * va_list version of PLy_elog_impl(), for callers that pass a message
+ * through from their own variadic argument list.
+ */
+void
+PLy_elog_impl_v(int elevel, const char *fmt, va_list ap)
 {
 	int			save_errno = errno;
 	char	   *volatile xmsg = NULL;
@@ -97,13 +111,13 @@ PLy_elog_impl(int elevel, const char *fmt, ...)
 		{
 			for (;;)
 			{
-				va_list		ap;
+				va_list		ap2;
 				int			needed;
 
 				errno = save_errno;
-				va_start(ap, fmt);
-				needed = appendStringInfoVA(&emsg, dgettext(TEXTDOMAIN, fmt), ap);
-				va_end(ap);
+				va_copy(ap2, ap);
+				needed = appendStringInfoVA(&emsg, dgettext(TEXTDOMAIN, fmt), ap2);
+				va_end(ap2);
 				if (needed == 0)
 					break;
 				enlargeStringInfo(&emsg, needed);
diff --git a/src/pl/plpython/plpy_elog.h b/src/pl/plpython/plpy_elog.h
index 887535d5860..ccc07f44f05 100644
--- a/src/pl/plpython/plpy_elog.h
+++ b/src/pl/plpython/plpy_elog.h
@@ -36,6 +36,8 @@ extern PyObject *PLy_exc_spi_error;
 
 extern PGDLLEXPORT void PLy_elog_impl(int elevel, const char *fmt, ...) pg_attribute_printf(2, 3);
 
+extern void PLy_elog_impl_v(int elevel, const char *fmt, va_list ap) pg_attribute_printf(2, 0);
+
 extern PGDLLEXPORT void PLy_exception_set(PyObject *exc, const char *fmt, ...) pg_attribute_printf(2, 3);
 
 extern PGDLLEXPORT void PLy_exception_set_plural(PyObject *exc, const char *fmt_singular, const char *fmt_plural,
diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c
index 14e63625070..d31a072ead6 100644
--- a/src/pl/plpython/plpy_exec.c
+++ b/src/pl/plpython/plpy_exec.c
@@ -28,6 +28,8 @@ static void plpython_return_error_callback(void *arg);
 
 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc,
 										HeapTuple *rv);
+static void PLyTD_SetItem(PyObject *td, const char *key, PyObject *value);
+static void PLyTD_SetItemNone(PyObject *td, const char *key);
 static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd,
 								  TriggerData *tdata, HeapTuple otup);
 static void plpython_trigger_error_callback(void *arg);
@@ -519,12 +521,10 @@ PLy_exec_event_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
 			PLy_elog(ERROR, NULL);
 
 		pltevent = PLyUnicode_FromString(tdata->event);
-		PyDict_SetItemString(pltdata, "event", pltevent);
-		Py_DECREF(pltevent);
+		PLyTD_SetItem(pltdata, "event", pltevent);
 
 		plttag = PLyUnicode_FromString(GetCommandTagName(tdata->tag));
-		PyDict_SetItemString(pltdata, "tag", plttag);
-		Py_DECREF(plttag);
+		PLyTD_SetItem(pltdata, "tag", plttag);
 
 		PLy_procedure_call(proc, "TD", pltdata);
 
@@ -561,25 +561,28 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
 		for (i = 0; i < proc->nargs; i++)
 		{
 			PLyDatumToOb *arginfo = &proc->args[i];
+			PyObject   *value;
 
 			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");
+			/*
+			 * PLyList_SetItem() consumes the reference even when the store
+			 * fails, so drop our pointer before the call.
+			 */
+			value = arg;
+			arg = NULL;
+			PLyList_SetItem(args, i, value,
+							"PyList_SetItem() failed, while setting up arguments");
 
 			if (proc->argnames && proc->argnames[i] &&
-				PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
+				PyDict_SetItemString(proc->globals, proc->argnames[i], value) < 0)
 				PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
-			arg = NULL;
 		}
 	}
 	PG_CATCH();
@@ -791,6 +794,25 @@ plpython_return_error_callback(void *arg)
 		errcontext("while creating return value");
 }
 
+/*
+ * Insert value into the TD dictionary under key, consuming the caller's
+ * reference.
+ */
+static void
+PLyTD_SetItem(PyObject *td, const char *key, PyObject *value)
+{
+	PLyDict_SetItemString(td, key, value, "could not build TD dictionary");
+}
+
+/*
+ * Insert the borrowed Py_None singleton into the TD dictionary under key.
+ */
+static void
+PLyTD_SetItemNone(PyObject *td, const char *key)
+{
+	PLyDict_SetItemNone(td, key, "could not build TD dictionary");
+}
+
 static PyObject *
 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
 {
@@ -803,10 +825,15 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 			   *pltrelid,
 			   *plttablename,
 			   *plttableschema,
-			   *pltargs,
 			   *pytnew,
 			   *pytold,
 			   *pltdata;
+
+	/*
+	 * pltargs is cleared inside the PG_TRY block once its reference has been
+	 * handed to the TD dictionary, so the PG_CATCH block must see that store.
+	 */
+	PyObject   *volatile pltargs;
 	char	   *stroid;
 
 	/*
@@ -835,27 +862,25 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 
 	PG_TRY();
 	{
+		PyObject   *args;
+
 		pltname = PLyUnicode_FromString(tdata->tg_trigger->tgname);
-		PyDict_SetItemString(pltdata, "name", pltname);
-		Py_DECREF(pltname);
+		PLyTD_SetItem(pltdata, "name", pltname);
 
 		stroid = DatumGetCString(DirectFunctionCall1(oidout,
 													 ObjectIdGetDatum(tdata->tg_relation->rd_id)));
 		pltrelid = PLyUnicode_FromString(stroid);
-		PyDict_SetItemString(pltdata, "relid", pltrelid);
-		Py_DECREF(pltrelid);
+		PLyTD_SetItem(pltdata, "relid", pltrelid);
 		pfree(stroid);
 
 		stroid = SPI_getrelname(tdata->tg_relation);
 		plttablename = PLyUnicode_FromString(stroid);
-		PyDict_SetItemString(pltdata, "table_name", plttablename);
-		Py_DECREF(plttablename);
+		PLyTD_SetItem(pltdata, "table_name", plttablename);
 		pfree(stroid);
 
 		stroid = SPI_getnspname(tdata->tg_relation);
 		plttableschema = PLyUnicode_FromString(stroid);
-		PyDict_SetItemString(pltdata, "table_schema", plttableschema);
-		Py_DECREF(plttableschema);
+		PLyTD_SetItem(pltdata, "table_schema", plttableschema);
 		pfree(stroid);
 
 		if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
@@ -869,14 +894,12 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 			elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
 			pltwhen = NULL;		/* keep compiler quiet */
 		}
-		PyDict_SetItemString(pltdata, "when", pltwhen);
-		Py_DECREF(pltwhen);
+		PLyTD_SetItem(pltdata, "when", pltwhen);
 
 		if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
 		{
 			pltlevel = PLyUnicode_FromString("ROW");
-			PyDict_SetItemString(pltdata, "level", pltlevel);
-			Py_DECREF(pltlevel);
+			PLyTD_SetItem(pltdata, "level", pltlevel);
 
 			/*
 			 * Note: In BEFORE trigger, stored generated columns are not
@@ -887,26 +910,24 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 			{
 				pltevent = PLyUnicode_FromString("INSERT");
 
-				PyDict_SetItemString(pltdata, "old", Py_None);
+				PLyTD_SetItemNone(pltdata, "old");
 				pytnew = PLy_input_from_tuple(&proc->result_in,
 											  tdata->tg_trigtuple,
 											  rel_descr,
 											  !TRIGGER_FIRED_BEFORE(tdata->tg_event));
-				PyDict_SetItemString(pltdata, "new", pytnew);
-				Py_DECREF(pytnew);
+				PLyTD_SetItem(pltdata, "new", pytnew);
 				*rv = tdata->tg_trigtuple;
 			}
 			else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
 			{
 				pltevent = PLyUnicode_FromString("DELETE");
 
-				PyDict_SetItemString(pltdata, "new", Py_None);
+				PLyTD_SetItemNone(pltdata, "new");
 				pytold = PLy_input_from_tuple(&proc->result_in,
 											  tdata->tg_trigtuple,
 											  rel_descr,
 											  true);
-				PyDict_SetItemString(pltdata, "old", pytold);
-				Py_DECREF(pytold);
+				PLyTD_SetItem(pltdata, "old", pytold);
 				*rv = tdata->tg_trigtuple;
 			}
 			else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
@@ -917,14 +938,12 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 											  tdata->tg_newtuple,
 											  rel_descr,
 											  !TRIGGER_FIRED_BEFORE(tdata->tg_event));
-				PyDict_SetItemString(pltdata, "new", pytnew);
-				Py_DECREF(pytnew);
+				PLyTD_SetItem(pltdata, "new", pytnew);
 				pytold = PLy_input_from_tuple(&proc->result_in,
 											  tdata->tg_trigtuple,
 											  rel_descr,
 											  true);
-				PyDict_SetItemString(pltdata, "old", pytold);
-				Py_DECREF(pytold);
+				PLyTD_SetItem(pltdata, "old", pytold);
 				*rv = tdata->tg_newtuple;
 			}
 			else
@@ -933,17 +952,15 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 				pltevent = NULL;	/* keep compiler quiet */
 			}
 
-			PyDict_SetItemString(pltdata, "event", pltevent);
-			Py_DECREF(pltevent);
+			PLyTD_SetItem(pltdata, "event", pltevent);
 		}
 		else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
 		{
 			pltlevel = PLyUnicode_FromString("STATEMENT");
-			PyDict_SetItemString(pltdata, "level", pltlevel);
-			Py_DECREF(pltlevel);
+			PLyTD_SetItem(pltdata, "level", pltlevel);
 
-			PyDict_SetItemString(pltdata, "old", Py_None);
-			PyDict_SetItemString(pltdata, "new", Py_None);
+			PLyTD_SetItemNone(pltdata, "old");
+			PLyTD_SetItemNone(pltdata, "new");
 			*rv = NULL;
 
 			if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
@@ -960,8 +977,7 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 				pltevent = NULL;	/* keep compiler quiet */
 			}
 
-			PyDict_SetItemString(pltdata, "event", pltevent);
-			Py_DECREF(pltevent);
+			PLyTD_SetItem(pltdata, "event", pltevent);
 		}
 		else
 			elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
@@ -980,19 +996,18 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
 			for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
 			{
 				pltarg = PLyUnicode_FromString(tdata->tg_trigger->tgargs[i]);
-
-				/*
-				 * stolen, don't Py_DECREF
-				 */
-				PyList_SetItem(pltargs, i, pltarg);
+				PLyList_SetItem(pltargs, i, pltarg,
+								"could not build TD dictionary");
 			}
 		}
 		else
 		{
 			Assert(pltargs == Py_None);
 		}
-		PyDict_SetItemString(pltdata, "args", pltargs);
-		Py_DECREF(pltargs);
+		/* The store consumes the reference, so give up our pointer. */
+		args = pltargs;
+		pltargs = NULL;
+		PLyTD_SetItem(pltdata, "args", args);
 	}
 	PG_CATCH();
 	{
@@ -1154,7 +1169,13 @@ PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
 	PyObject   *rv = NULL;
 	int volatile save_subxact_level = list_length(explicit_subtransactions);
 
-	PyDict_SetItemString(proc->globals, kargs, vargs);
+	/*
+	 * vargs is NULL, with a Python exception set, if building it failed. The
+	 * caller still owns the reference, so do not steal it.
+	 */
+	if (vargs == NULL ||
+		PyDict_SetItemString(proc->globals, kargs, vargs) < 0)
+		PLy_elog(ERROR, "could not set up arguments for PL/Python function");
 
 	PG_TRY();
 	{
diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c
index e499ed8cbfd..a6490d99871 100644
--- a/src/pl/plpython/plpy_typeio.c
+++ b/src/pl/plpython/plpy_typeio.c
@@ -104,6 +104,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 +152,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;
 }
 
@@ -714,63 +720,79 @@ PLyList_FromArray_recurse(PLyDatumToOb *elm, int *dims, int ndim, int dim,
 	if (!list)
 		return NULL;
 
-	if (dim < ndim - 1)
+	/*
+	 * Filling the list can now throw, and nothing else refers to it yet, so
+	 * release it here rather than leaking it.
+	 */
+	PG_TRY();
 	{
-		/* Outer dimension. Recurse for each inner slice. */
-		for (i = 0; i < dims[dim]; i++)
+		if (dim < ndim - 1)
 		{
-			PyObject   *sublist;
+			/* Outer dimension. Recurse for each inner slice. */
+			for (i = 0; i < dims[dim]; i++)
+			{
+				PyObject   *sublist;
 
-			sublist = PLyList_FromArray_recurse(elm, dims, ndim, dim + 1,
-												dataptr_p, bitmap_p, bitmask_p);
-			PyList_SetItem(list, i, sublist);
+				sublist = PLyList_FromArray_recurse(elm, dims, ndim, dim + 1,
+													dataptr_p, bitmap_p, bitmask_p);
+				PLyList_SetItem(list, i, sublist,
+								"could not convert array dimension to a Python list");
+			}
 		}
-	}
-	else
-	{
-		/*
-		 * Innermost dimension. Fill the list with the values from the array
-		 * for this slice.
-		 */
-		char	   *dataptr = *dataptr_p;
-		uint8	   *bitmap = *bitmap_p;
-		int			bitmask = *bitmask_p;
-		uint8		typalignby = typalign_to_alignby(elm->typalign);
-
-		for (i = 0; i < dims[dim]; i++)
+		else
 		{
-			/* checking for NULL */
-			if (bitmap && (*bitmap & bitmask) == 0)
-			{
-				Py_INCREF(Py_None);
-				PyList_SetItem(list, i, Py_None);
-			}
-			else
+			/*
+			 * Innermost dimension. Fill the list with the values from the
+			 * array for this slice.
+			 */
+			char	   *dataptr = *dataptr_p;
+			uint8	   *bitmap = *bitmap_p;
+			int			bitmask = *bitmask_p;
+			uint8		typalignby = typalign_to_alignby(elm->typalign);
+
+			for (i = 0; i < dims[dim]; i++)
 			{
-				Datum		itemvalue;
+				/* checking for NULL */
+				if (bitmap && (*bitmap & bitmask) == 0)
+				{
+					Py_INCREF(Py_None);
+					PLyList_SetItem(list, i, Py_None,
+									"could not add array element to a Python list");
+				}
+				else
+				{
+					Datum		itemvalue;
 
-				itemvalue = fetch_att(dataptr, elm->typbyval, elm->typlen);
-				PyList_SetItem(list, i, elm->func(elm, itemvalue));
-				dataptr = att_addlength_pointer(dataptr, elm->typlen, dataptr);
-				dataptr = (char *) att_nominal_alignby(dataptr, typalignby);
-			}
+					itemvalue = fetch_att(dataptr, elm->typbyval, elm->typlen);
+					PLyList_SetItem(list, i, elm->func(elm, itemvalue),
+									"could not convert array element to a Python object");
+					dataptr = att_addlength_pointer(dataptr, elm->typlen, dataptr);
+					dataptr = (char *) att_nominal_alignby(dataptr, typalignby);
+				}
 
-			/* advance bitmap pointer if any */
-			if (bitmap)
-			{
-				bitmask <<= 1;
-				if (bitmask == 0x100 /* (1<<8) */ )
+				/* advance bitmap pointer if any */
+				if (bitmap)
 				{
-					bitmap++;
-					bitmask = 1;
+					bitmask <<= 1;
+					if (bitmask == 0x100 /* (1<<8) */ )
+					{
+						bitmap++;
+						bitmask = 1;
+					}
 				}
 			}
-		}
 
-		*dataptr_p = dataptr;
-		*bitmap_p = bitmap;
-		*bitmask_p = bitmask;
+			*dataptr_p = dataptr;
+			*bitmap_p = bitmap;
+			*bitmask_p = bitmask;
+		}
+	}
+	PG_CATCH();
+	{
+		Py_DECREF(list);
+		PG_RE_THROW();
 	}
+	PG_END_TRY();
 
 	return list;
 }
@@ -835,7 +857,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 +875,13 @@ 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);
+				PLyDict_SetItemNone(dict, key,
+									"could not add column \"%s\" to Python dictionary",
+									key);
 			else
-			{
-				value = att->func(att, vattr);
-				PyDict_SetItemString(dict, key, value);
-				Py_DECREF(value);
-			}
+				PLyDict_SetItemString(dict, key, att->func(att, vattr),
+									  "could not convert column \"%s\" to a Python object",
+									  key);
 		}
 	}
 	PG_CATCH();
@@ -1039,20 +1060,27 @@ PLyObject_AsString(PyObject *plrv)
 		/* use repr() for floats, str() is lossy */
 		PyObject   *s = PyObject_Repr(plrv);
 
+		if (s == NULL)
+			PLy_elog(ERROR, "could not create string representation of Python object");
 		plrv_bo = PLyUnicode_Bytes(s);
-		Py_XDECREF(s);
+		Py_DECREF(s);
 	}
 	else
 	{
 		PyObject   *s = PyObject_Str(plrv);
 
+		if (s == NULL)
+			PLy_elog(ERROR, "could not create string representation of Python object");
 		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 +1409,21 @@ 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, "could not get value for key \"%s\" from mapping",
+							 key);
+				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);
 
@@ -1536,6 +1574,15 @@ PLyGenericObject_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *object
 				 * 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, "could not get attribute \"%s\" of Python object",
+							 key);
+				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_typeio.h b/src/pl/plpython/plpy_typeio.h
index 29258509b5c..82541f2b89d 100644
--- a/src/pl/plpython/plpy_typeio.h
+++ b/src/pl/plpython/plpy_typeio.h
@@ -147,6 +147,10 @@ struct PLyObToDatum
 };
 
 
+/*
+ * PLy_input_convert and PLy_input_from_tuple throw an error rather than
+ * returning NULL when the Python object cannot be built.
+ */
 extern PGDLLEXPORT PyObject *PLy_input_convert(PLyDatumToOb *arg, Datum val);
 extern PGDLLEXPORT Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val,
 											bool *isnull);
diff --git a/src/pl/plpython/plpy_util.c b/src/pl/plpython/plpy_util.c
index c2338decba2..1b8a53f2401 100644
--- a/src/pl/plpython/plpy_util.c
+++ b/src/pl/plpython/plpy_util.c
@@ -117,3 +117,84 @@ PLyUnicode_FromString(const char *s)
 {
 	return PLyUnicode_FromStringAndSize(s, strlen(s));
 }
+
+/*
+ * Common part of PLyDict_SetItemString() and PLyDict_SetItemNone().
+ */
+static void
+PLyDict_SetItem_v(PyObject *dict, const char *key, PyObject *value,
+				  const char *fmt, va_list ap)
+{
+	if (value != NULL)
+	{
+		int			rc = PyDict_SetItemString(dict, key, value);
+
+		/* PyDict_SetItemString does not steal the reference */
+		Py_DECREF(value);
+
+		if (rc == 0)
+			return;
+	}
+
+	PLy_elog_impl_v(ERROR, fmt, ap);
+	pg_unreachable();
+}
+
+/*
+ * Insert value into dict under key, consuming the caller's reference.
+ *
+ * value is a new reference owned by the caller, or NULL with a Python
+ * exception set. Py_None is not special here: the caller must own a
+ * reference to it, or use PLyDict_SetItemNone(). A failed conversion and a
+ * failed insert are both reported under fmt.
+ */
+void
+PLyDict_SetItemString(PyObject *dict, const char *key, PyObject *value,
+					  const char *fmt, ...)
+{
+	va_list		ap;
+
+	va_start(ap, fmt);
+	PLyDict_SetItem_v(dict, key, value, fmt, ap);
+	va_end(ap);
+}
+
+/*
+ * Insert the borrowed Py_None singleton into dict under key.
+ */
+void
+PLyDict_SetItemNone(PyObject *dict, const char *key, const char *fmt, ...)
+{
+	va_list		ap;
+
+	Py_INCREF(Py_None);
+
+	va_start(ap, fmt);
+	PLyDict_SetItem_v(dict, key, Py_None, fmt, ap);
+	va_end(ap);
+}
+
+/*
+ * Store value at index i of list, consuming the caller's reference.
+ *
+ * value is a new reference owned by the caller, or NULL with a Python
+ * exception set. Py_None is not special here: the caller must own a
+ * reference to it. listobject.h says PyList_SetItem decrements the new
+ * reference even when the item is not inserted, so a failed store must not
+ * release it again. A failed conversion and a failed store are both
+ * reported under fmt.
+ */
+void
+PLyList_SetItem(PyObject *list, Py_ssize_t i, PyObject *value,
+				const char *fmt, ...)
+{
+	va_list		ap;
+
+	if (value != NULL && PyList_SetItem(list, i, value) == 0)
+		return;
+
+	va_start(ap, fmt);
+	PLy_elog_impl_v(ERROR, fmt, ap);
+	va_end(ap);
+	pg_unreachable();
+}
diff --git a/src/pl/plpython/plpy_util.h b/src/pl/plpython/plpy_util.h
index 6f491b0f95b..4665b123eae 100644
--- a/src/pl/plpython/plpy_util.h
+++ b/src/pl/plpython/plpy_util.h
@@ -14,4 +14,12 @@ 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 void PLyDict_SetItemString(PyObject *dict, const char *key,
+								  PyObject *value,
+								  const char *fmt, ...) pg_attribute_printf(4, 5);
+extern void PLyDict_SetItemNone(PyObject *dict, const char *key,
+								const char *fmt, ...) pg_attribute_printf(3, 4);
+extern void PLyList_SetItem(PyObject *list, Py_ssize_t i, PyObject *value,
+							const char *fmt, ...) pg_attribute_printf(4, 5);
+
 #endif							/* PLPY_UTIL_H */
diff --git a/src/pl/plpython/sql/plpython_record.sql b/src/pl/plpython/sql/plpython_record.sql
index bc1e7d54c8a..a07b4264c41 100644
--- a/src/pl/plpython/sql/plpython_record.sql
+++ b/src/pl/plpython/sql/plpython_record.sql
@@ -178,3 +178,25 @@ CREATE FUNCTION test_type_record_error4() RETURNS type_record AS $$
 $$ LANGUAGE plpython3u;
 
 SELECT * FROM test_type_record_error4();
+
+-- a mapping that fails for a reason other than a missing key
+CREATE FUNCTION test_type_record_error5() RETURNS type_record AS $$
+    class broken_mapping(dict):
+        def __getitem__(self, key):
+            raise TypeError('broken mapping')
+    return broken_mapping(first='first', second=1)
+$$ LANGUAGE plpython3u;
+
+SELECT * FROM test_type_record_error5();
+
+-- an object whose attribute lookup fails for a reason other than a
+-- missing attribute
+CREATE FUNCTION test_type_record_error6() RETURNS type_record AS $$
+    class broken_object:
+        first = 'first'
+        def __getattr__(self, name):
+            raise ValueError('broken attribute')
+    return broken_object()
+$$ LANGUAGE plpython3u;
+
+SELECT * FROM test_type_record_error6();
-- 
2.53.0

