Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Handle zero-length sublist correctly in Python -> SQL array conversion.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 28 Apr 2023 16:24:29 +0000 (12:24 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 28 Apr 2023 16:24:29 +0000 (12:24 -0400)
If PLySequence_ToArray came across a zero-length sublist, it'd compute
the overall array size as zero, possibly leading to a memory clobber.
(This would likely qualify as a security bug, were it not that plpython
is an untrusted language already.)

I think there are other corner-case issues in this code as well, notably
that the error messages don't match the core code and for some ranges
of array sizes you'd get "invalid memory alloc request size" rather than
the intended message about array size.

Really this code has no business doing its own array size calculation
at all, so remove the faulty code in favor of using ArrayGetNItems().

Per bug #17912 from Alexander Lakhin.  Bug seems to have come in with
commit 94aceed31, so back-patch to all supported branches.

Discussion: https://postgr.es/m/17912-82ceed78731d9cdc@postgresql.org

src/pl/plpython/expected/plpython_types.out
src/pl/plpython/expected/plpython_types_3.out
src/pl/plpython/plpy_typeio.c
src/pl/plpython/sql/plpython_types.sql

index eda965a9e0d7f8810a67ced583457ff799e65483..bae02e9c04ed5b0970a12f6bf5688a70a51e7a21 100644 (file)
@@ -687,6 +687,15 @@ SELECT * FROM test_type_conversion_array_mixed2();
 ERROR:  invalid input syntax for integer: "abc"
 CONTEXT:  while creating return value
 PL/Python function "test_type_conversion_array_mixed2"
+CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
+return [[], 'a']
+$$ LANGUAGE plpythonu;
+SELECT * FROM test_type_conversion_array_mixed3();
+ test_type_conversion_array_mixed3 
+-----------------------------------
+ {[],a}
+(1 row)
+
 CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
 return [[1,2,3],[4,5]]
 $$ LANGUAGE plpythonu;
index 69f958cbf288539cf44ace6497a6fa6ee4ccfa8f..9049faaaf960e48ba0c253c8c4d4e0cb87141dcd 100644 (file)
@@ -687,6 +687,15 @@ SELECT * FROM test_type_conversion_array_mixed2();
 ERROR:  invalid input syntax for integer: "abc"
 CONTEXT:  while creating return value
 PL/Python function "test_type_conversion_array_mixed2"
+CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
+return [[], 'a']
+$$ LANGUAGE plpython3u;
+SELECT * FROM test_type_conversion_array_mixed3();
+ test_type_conversion_array_mixed3 
+-----------------------------------
+ {[],a}
+(1 row)
+
 CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
 return [[1,2,3],[4,5]]
 $$ LANGUAGE plpython3u;
index 62c46d9aabc8546db008667472a4f45713a2c521..6edef990652ae604dc7ba4d46f93cc0b3d911984 100644 (file)
@@ -1144,7 +1144,7 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
    int         i;
    Datum      *elems;
    bool       *nulls;
-   int64       len;
+   int         len;
    int         ndim;
    int         dims[MAXDIM];
    int         lbs[MAXDIM];
@@ -1163,7 +1163,6 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
     * Determine the number of dimensions, and their sizes.
     */
    ndim = 0;
-   len = 1;
 
    Py_INCREF(plrv);
 
@@ -1179,13 +1178,6 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
        if (dims[ndim] < 0)
            PLy_elog(ERROR, "could not determine sequence length for function return value");
 
-       if (dims[ndim] > MaxAllocSize)
-           PLy_elog(ERROR, "array size exceeds the maximum allowed");
-
-       len *= dims[ndim];
-       if (len > MaxAllocSize)
-           PLy_elog(ERROR, "array size exceeds the maximum allowed");
-
        if (dims[ndim] == 0)
        {
            /* empty sequence */
@@ -1213,15 +1205,18 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
            PLy_elog(ERROR, "return value of function with array return type is not a Python sequence");
 
        ndim = 1;
-       len = dims[0] = PySequence_Length(plrv);
+       dims[0] = PySequence_Length(plrv);
    }
 
+   /* Allocate space for work arrays, after detecting array size overflow */
+   len = ArrayGetNItems(ndim, dims);
+   elems = palloc(sizeof(Datum) * len);
+   nulls = palloc(sizeof(bool) * len);
+
    /*
     * Traverse the Python lists, in depth-first order, and collect all the
     * elements at the bottom level into 'elems'/'nulls' arrays.
     */
-   elems = palloc(sizeof(Datum) * len);
-   nulls = palloc(sizeof(bool) * len);
    currelem = 0;
    PLySequence_ToArray_recurse(arg->u.array.elm, plrv,
                                dims, ndim, 0,
index cc0524ee806bddcc94d031d6c39ac7a132728060..8fa8f6bee7f3b700d73be3f973beb11eb5a6615c 100644 (file)
@@ -328,6 +328,13 @@ $$ LANGUAGE plpythonu;
 
 SELECT * FROM test_type_conversion_array_mixed2();
 
+CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
+return [[], 'a']
+$$ LANGUAGE plpythonu;
+
+SELECT * FROM test_type_conversion_array_mixed3();
+
+
 CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
 return [[1,2,3],[4,5]]
 $$ LANGUAGE plpythonu;