pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Lakhin <exclusion@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17912: Invalid memory access when converting plpython' array containing empty array
Date: Fri, 28 Apr 2023 21:00:00 +0300
Message-ID: <bf33fe9b-f2f8-5885-aae3-537cd4591386@gmail.com> (raw)
In-Reply-To: <1392974.1682694846@sss.pgh.pa.us>
References: <17912-82ceed78731d9cdc@postgresql.org>
	<1392974.1682694846@sss.pgh.pa.us>

Hello Tom,

28.04.2023 18:14, Tom Lane wrote:
> PG Bug reporting form <noreply@postgresql.org> writes:
>> CREATE EXTENSION plpython3u;
>> CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$
>> return [[], "a"]
>> $$ LANGUAGE plpython3u;
>> SELECT test();
>> As I can see, for the first case we get len = 0 in PLySequence_ToArray();
>> elems, nulls palloc'ed with zero elements, but PLyObject_ToScalar() tries
>> to write a value into nulls[0]...
> Yeah.  The calculation of the array size is being done in the wrong
> place, so that we may update len to zero before breaking out of the
> loop.  But really it's poor coding for this function to be doing
> its own calculation of the array size at all, rather than consulting
> the authoritative ArrayGetNItems().  I think we need something like
> the attached.

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();
---------
  {1,[2]}

But:
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.
CONTEXT:  while creating return value
PL/Python function "test"

Or:
CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$
return [["1"], "abc"]
$$ LANGUAGE plpython3u;
SELECT test();
ERROR:  wrong length of inner sequence: has length 3, but 1 was expected
DETAIL:  To construct a multidimensional array, the inner sequences must all have the same length.
CONTEXT:  while creating return value
PL/Python function "test"

Though may be it's okay, considering python's concepts of
strings/arrays/sequences, or at least deserves a separate discussion.

Best regards,
Alexander





view thread (12+ messages)  latest in thread

Message-ID: <bf33fe9b-f2f8-5885-aae3-537cd4591386@gmail.com>
Permalink:  ../bf33fe9b-f2f8-5885-aae3-537cd4591386@gmail.com/
Also on:    postgresql.org/message-id/bf33fe9b-f2f8-5885-aae3-537cd4591386@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: exclusion@gmail.com, tgl@sss.pgh.pa.us, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #17912: Invalid memory access when converting plpython' array containing empty array
  In-Reply-To: <bf33fe9b-f2f8-5885-aae3-537cd4591386@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox