From ce9662598ddac1883d921d47a3fde63859f077ed Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 27 Apr 2023 11:55:06 -0400 Subject: In hstore_plpython, avoid crashing when return value isn't a mapping. Python 3 changed the behavior of PyMapping_Check(), breaking the test in plpython_to_hstore() that verifies whether a function result to be transformed is acceptable. A backwards-compatible fix is to first verify that the object doesn't pass PySequence_Check(). Perhaps accidentally, our other uses of PyMapping_Check() already follow uses of PySequence_Check(), so that no other bugs were created by this change. Per bug #17908 from Alexander Lakhin. Back-patch to all supported branches. Dmitry Dolgov and Tom Lane Discussion: https://postgr.es/m/17908-3f19a125d56a11d6@postgresql.org --- contrib/hstore_plpython/hstore_plpython.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'contrib/hstore_plpython/hstore_plpython.c') diff --git a/contrib/hstore_plpython/hstore_plpython.c b/contrib/hstore_plpython/hstore_plpython.c index 93705f0d54c..65ab2053f74 100644 --- a/contrib/hstore_plpython/hstore_plpython.c +++ b/contrib/hstore_plpython/hstore_plpython.c @@ -133,7 +133,13 @@ plpython_to_hstore(PG_FUNCTION_ARGS) HStore *volatile out; dict = (PyObject *) PG_GETARG_POINTER(0); - if (!PyMapping_Check(dict)) + + /* + * As of Python 3, PyMapping_Check() is unreliable unless one first checks + * that the object isn't a sequence. (Cleaner solutions exist, but not + * before Python 3.10, which we're not prepared to require yet.) + */ + if (PySequence_Check(dict) || !PyMapping_Check(dict)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("not a Python mapping"))); -- cgit v1.2.3