Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitff9203f

Browse files
committed
Handle zero-length sublist correctly in Python -> SQL array conversion.
If PLySequence_ToArray came across a zero-length sublist, it'd computethe overall array size as zero, possibly leading to a memory clobber.(This would likely qualify as a security bug, were it not that plpythonis an untrusted language already.)I think there are other corner-case issues in this code as well, notablythat the error messages don't match the core code and for some rangesof array sizes you'd get "invalid memory alloc request size" rather thanthe intended message about array size.Really this code has no business doing its own array size calculationat all, so remove the faulty code in favor of using ArrayGetNItems().Per bug #17912 from Alexander Lakhin. Bug seems to have come in withcommit94aceed, so back-patch to all supported branches.Discussion:https://postgr.es/m/17912-82ceed78731d9cdc@postgresql.org
1 parent63f7e91 commitff9203f

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

‎src/pl/plpython/expected/plpython_types.out

Lines changed: 9 additions & 0 deletions
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 plpythonu;
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 plpythonu;

‎src/pl/plpython/expected/plpython_types_3.out

Lines changed: 9 additions & 0 deletions
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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
11511151
inti;
11521152
Datum*elems;
11531153
bool*nulls;
1154-
int64len;
1154+
intlen;
11551155
intndim;
11561156
intdims[MAXDIM];
11571157
intlbs[MAXDIM];
@@ -1170,7 +1170,6 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
11701170
* Determine the number of dimensions, and their sizes.
11711171
*/
11721172
ndim=0;
1173-
len=1;
11741173

11751174
Py_INCREF(plrv);
11761175

@@ -1186,13 +1185,6 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
11861185
if (dims[ndim]<0)
11871186
PLy_elog(ERROR,"could not determine sequence length for function return value");
11881187

1189-
if (dims[ndim]>MaxAllocSize)
1190-
PLy_elog(ERROR,"array size exceeds the maximum allowed");
1191-
1192-
len *=dims[ndim];
1193-
if (len>MaxAllocSize)
1194-
PLy_elog(ERROR,"array size exceeds the maximum allowed");
1195-
11961188
if (dims[ndim]==0)
11971189
{
11981190
/* empty sequence */
@@ -1220,15 +1212,18 @@ PLySequence_ToArray(PLyObToDatum *arg, PyObject *plrv,
12201212
PLy_elog(ERROR,"return value of function with array return type is not a Python sequence");
12211213

12221214
ndim=1;
1223-
len=dims[0]=PySequence_Length(plrv);
1215+
dims[0]=PySequence_Length(plrv);
12241216
}
12251217

1218+
/* Allocate space for work arrays, after detecting array size overflow */
1219+
len=ArrayGetNItems(ndim,dims);
1220+
elems=palloc(sizeof(Datum)*len);
1221+
nulls=palloc(sizeof(bool)*len);
1222+
12261223
/*
12271224
* Traverse the Python lists, in depth-first order, and collect all the
12281225
* elements at the bottom level into 'elems'/'nulls' arrays.
12291226
*/
1230-
elems=palloc(sizeof(Datum)*len);
1231-
nulls=palloc(sizeof(bool)*len);
12321227
currelem=0;
12331228
PLySequence_ToArray_recurse(arg->u.array.elm,plrv,
12341229
dims,ndim,0,

‎src/pl/plpython/sql/plpython_types.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ $$ LANGUAGE plpythonu;
328328

329329
SELECT*FROM test_type_conversion_array_mixed2();
330330

331+
CREATEFUNCTIONtest_type_conversion_array_mixed3() RETURNStext[]AS $$
332+
return [[],'a']
333+
$$ LANGUAGE plpythonu;
334+
335+
SELECT*FROM test_type_conversion_array_mixed3();
336+
337+
331338
CREATEFUNCTIONtest_type_conversion_mdarray_malformed() RETURNSint[]AS $$
332339
return [[1,2,3],[4,5]]
333340
$$ LANGUAGE plpythonu;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp