Re: BUG #17908: plpython_to_hstore() crashes with a non-dict argument

From: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>
To: exclusion(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17908: plpython_to_hstore() crashes with a non-dict argument
Date: 2023-04-27 08:27:27
Message-ID: 20230427082727.4na67kulnnlxkzq3@ddolgov.remote.csb
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

> On Tue, Apr 25, 2023 at 01:00:02PM +0000, PG Bug reporting form wrote:
> The following bug has been logged on the website:
>
> Bug reference: 17908
> Logged by: Alexander Lakhin
> Email address: exclusion(at)gmail(dot)com
> PostgreSQL version: 15.2
> Operating system: Ubuntu 22.04
> Description:
>
> When executing the following query:
> CREATE EXTENSION hstore_plpython3u CASCADE;
> CREATE FUNCTION test_transform() RETURNS hstore
> LANGUAGE plpython3u
> TRANSFORM FOR TYPE hstore AS 'return "a"';
> SELECT test_transform();
>
> Here plpython_to_hstore() has got a string as an argument, but failed to
> detect that it's not a dictionary.
> With plpython2 (on REL_14_STABLE) I get:
> ERROR: not a Python mapping
> CONTEXT: while creating return value
> PL/Python function "test_transform"
>
> As noted in https://bugs.python.org/issue5945, the behaviour of
> PyMapping_Check() changed in Python 3, so it can't be used anymore to
> differentiate real dictionaries from strings or alike.

Thanks for finding this! That's indeed very annoying. After a quick
investigation looks like a proposed solution from [1] is to use
PyType_HasFeature with Py_TPFLAGS_MAPPING:

- if (!PyMapping_Check(dict))
+ dict_type = Py_TYPE(dict);
+ if (!PyType_HasFeature(dict_type, Py_TPFLAGS_MAPPING))

But this flag is available only starting from Python 3.10, so not
backward compatible :(

[1]: https://bugs.python.org/issue46376

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2023-04-27 08:45:15 BUG #17910: gcc-introduced load may cause concurrency bug
Previous Message Sandeep Thakkar 2023-04-27 07:43:00 Re: BUG #17907: PostgresSQL 15.x contains OpenSSL DLLs (vulnerable to CVE-2023-0464, CVE-2023-0465 & CVE-2023-0466)