Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 81eaaf6

Browse files
committed
Handle zero-length sublist correctly in Python -> SQL array conversion.
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 94aceed, so back-patch to all supported branches. Discussion: https://postgr.es/m/17912-82ceed78731d9cdc@postgresql.org
1 parent 4dadd66 commit 81eaaf6

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

src/pl/plpython/expected/plpython_types.out

+9
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,15 @@ SELECT * FROM test_type_conversion_array_mixed2();
687687
ERROR: invalid input syntax for type integer: "abc"
688688
CONTEXT: while creating return value
689689
PL/Python function "test_type_conversion_array_mixed2"
690+
CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
691+
return [[], 'a']
692+
$$ LANGUAGE plpython3u;
693+
SELECT * FROM test_type_conversion_array_mixed3();
694+
test_type_conversion_array_mixed3
695+
-----------------------------------
696+
{[],a}
697+
(1 row)
698+
690699
CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
691700
return [[1,2,3],[4,5]]
692701
$$ LANGUAGE plpython3u;

src/pl/plpython/plpy_typeio.c

+7-16
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
11361136
int i;
11371137
Datum *elems;
11381138
bool *nulls;
1139-
int64 len;
1139+
int len;
11401140
int ndim;
11411141
int dims[MAXDIM];
11421142
int lbs[MAXDIM];
@@ -1155,7 +1155,6 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
11551155
* Determine the number of dimensions, and their sizes.
11561156
*/
11571157
ndim = 0;
1158-
len = 1;
11591158

11601159
Py_INCREF(plrv);
11611160

@@ -1174,17 +1173,6 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
11741173
if (dims[ndim] < 0)
11751174
PLy_elog(ERROR, "could not determine sequence length for function return value");
11761175

1177-
if (dims[ndim] > MaxAllocSize)
1178-
ereport(ERROR,
1179-
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1180-
errmsg("array size exceeds the maximum allowed")));
1181-
1182-
len *= dims[ndim];
1183-
if (len > MaxAllocSize)
1184-
ereport(ERROR,
1185-
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1186-
errmsg("array size exceeds the maximum allowed")));
1187-
11881176
if (dims[ndim] == 0)
11891177
{
11901178
/* empty sequence */
@@ -1214,15 +1202,18 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
12141202
errmsg("return value of function with array return type is not a Python sequence")));
12151203

12161204
ndim = 1;
1217-
len = dims[0] = PySequence_Length(plrv);
1205+
dims[0] = PySequence_Length(plrv);
12181206
}
12191207

1208+
/* Allocate space for work arrays, after detecting array size overflow */
1209+
len = ArrayGetNItems(ndim, dims);
1210+
elems = palloc(sizeof(Datum) * len);
1211+
nulls = palloc(sizeof(bool) * len);
1212+
12201213
/*
12211214
* Traverse the Python lists, in depth-first order, and collect all the
12221215
* elements at the bottom level into 'elems'/'nulls' arrays.
12231216
*/
1224-
elems = palloc(sizeof(Datum) * len);
1225-
nulls = palloc(sizeof(bool) * len);
12261217
currelem = 0;
12271218
PLySequence_ToArray_recurse(arg->u.array.elm, plrv,
12281219
dims, ndim, 0,

src/pl/plpython/sql/plpython_types.sql

+7
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ $$ LANGUAGE plpython3u;
328328

329329
SELECT * FROM test_type_conversion_array_mixed2();
330330

331+
CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
332+
return [[], 'a']
333+
$$ LANGUAGE plpython3u;
334+
335+
SELECT * FROM test_type_conversion_array_mixed3();
336+
337+
331338
CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
332339
return [[1,2,3],[4,5]]
333340
$$ LANGUAGE plpython3u;

0 commit comments

Comments
 (0)