Tighten array dimensionality checks in Python -> SQL array conversion.

Like plperl before f47004add, plpython wasn't being sufficiently
careful about checking that list-of-list structures represent
rectangular arrays, so that it would accept some cases in which
different parts of the "array" are nested to different depths.
This was exacerbated by Python's weak distinction between
sequences and lists, so that in some cases strings could get
treated as though they are lists (and burst into individual
characters) even though a different ordering of the upper-level
list would give a different result.

Some of this behavior was unreachable (without risking a crash)
before 81eaaf65e.  It seems like a good idea to clean it all up
in the same releases, rather than shipping a non-crashing but
nonetheless visibly buggy behavior in the name of minimal change.
Hence, back-patch.

Per bug #17912 and further testing by Alexander Lakhin.

Discussion: https://postgr.es/m/17912-82ceed78731d9cdc@postgresql.org
This commit is contained in:
Tom Lane
2023-05-04 11:00:33 -04:00
parent 23c7aa865b
commit b7001c6b6a
3 changed files with 212 additions and 121 deletions

View File

@ -328,11 +328,43 @@ $$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_array_mixed2();
CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
return [[], 'a']
-- check output of multi-dimensional arrays
CREATE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
return [['a'], ['b'], ['c']]
$$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_array_mixed3();
select test_type_conversion_md_array_out();
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
return [[], []]
$$ LANGUAGE plpython3u;
select test_type_conversion_md_array_out();
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
return [[], [1]]
$$ LANGUAGE plpython3u;
select test_type_conversion_md_array_out(); -- fail
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
return [[], 1]
$$ LANGUAGE plpython3u;
select test_type_conversion_md_array_out(); -- fail
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
return [1, []]
$$ LANGUAGE plpython3u;
select test_type_conversion_md_array_out(); -- fail
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
return [[1], [[]]]
$$ LANGUAGE plpython3u;
select test_type_conversion_md_array_out(); -- fail
CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
@ -341,6 +373,18 @@ $$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_mdarray_malformed();
CREATE FUNCTION test_type_conversion_mdarray_malformed2() RETURNS text[] AS $$
return [[1,2,3], "abc"]
$$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_mdarray_malformed2();
CREATE FUNCTION test_type_conversion_mdarray_malformed3() RETURNS text[] AS $$
return ["abc", [1,2,3]]
$$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_mdarray_malformed3();
CREATE FUNCTION test_type_conversion_mdarray_toodeep() RETURNS int[] AS $$
return [[[[[[[1]]]]]]]
$$ LANGUAGE plpython3u;