Re: BUG #17912: Invalid memory access when converting plpython' array containing empty array

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Lakhin <exclusion(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17912: Invalid memory access when converting plpython' array containing empty array
Date: 2023-04-28 18:18:36
Message-ID: 1440309.1682705916@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Alexander Lakhin <exclusion(at)gmail(dot)com> writes:
> Thank you! I've read the message of the commit you just pushed,
> and I confirm that there are another oddities in that area. For example:

> CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$
> return [[1], 2]
> $$ LANGUAGE plpython3u;
> SELECT test();
> ERROR:  wrong length of inner sequence: has length -1, but 1 was expected
> DETAIL:  To construct a multidimensional array, the inner sequences must all have the same length.

Yeah. AFAICT, the idea of the existing code is to descend through
the first item at each nest level till we hit a non-list, then take
the lengths of those lists as the array dimensions, and then complain
if we find any later items that don't fit those dimensions. That leads
to some symmetry problems, in that the error you get for inconsistent
sequence lengths depends on the order in which the items are presented.

Also, it seems like there is something odd here:

regression=# CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$
regression$# return ["abc"]
regression$# $$ LANGUAGE plpython3u;
CREATE FUNCTION
regression=# select test();
test
-------
{abc}
(1 row)

regression=# CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$
return [[1,2,3], "abc"]
$$ LANGUAGE plpython3u;
CREATE FUNCTION
regression=# select test();
test
-------------------
{{1,2,3},{a,b,c}}
(1 row)

I think it's weird that "abc" is taken as a scalar in the first
case and a list in the second case. Even worse,

regression=# CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$
return ["abc", [1,2,3]]
$$ LANGUAGE plpython3u;
CREATE FUNCTION
regression=# select test();
test
-------------------
{abc,"[1, 2, 3]"}
(1 row)

This might be something that used to work more sanely with
Python 2 and got broken in Python 3 for the same reasons
discussed in bug #17908. I've not poked at it more though.
I don't think I have a Python 2 installation to test with
anymore :-(

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Bruce Momjian 2023-04-28 18:47:57 Re: BUG #16628: Hostame and string connection functions
Previous Message Alexander Lakhin 2023-04-28 18:00:00 Re: BUG #17912: Invalid memory access when converting plpython' array containing empty array