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

Commit cd331e4

Browse files
committed
Defend against possible crash if a plpython function does not specify names
for its arguments. Also add a regression test, since someone apparently changed every single plpython test case to use only named parameters; else we'd have noticed this sooner. Euler Taveira de Oliveira, per a report from Alvaro
1 parent aa38153 commit cd331e4

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

src/pl/plpython/expected/plpython_function.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,11 @@ CREATE FUNCTION test_return_none() RETURNS int AS $$
357357
None
358358
$$ LANGUAGE plpythonu;
359359
--
360-
-- Test named parameters
360+
-- Test named and nameless parameters
361361
--
362+
CREATE FUNCTION test_param_names0(integer, integer) RETURNS int AS $$
363+
return args[0] + args[1]
364+
$$ LANGUAGE plpythonu;
362365
CREATE FUNCTION test_param_names1(a0 integer, a1 text) RETURNS boolean AS $$
363366
assert a0 == args[0]
364367
assert a1 == args[1]

src/pl/plpython/expected/plpython_test.out

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ SELECT test_return_none(), test_return_none() IS NULL AS "is null";
197197
| t
198198
(1 row)
199199

200-
-- Test for functions with named parameters
200+
-- Test for functions with named and nameless parameters
201+
SELECT test_param_names0(2,7);
202+
test_param_names0
203+
-------------------
204+
9
205+
(1 row)
206+
201207
SELECT test_param_names1(1,'text');
202208
test_param_names1
203209
-------------------

src/pl/plpython/plpython.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**********************************************************************
22
* plpython.c - python as a procedural language for PostgreSQL
33
*
4-
* $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.119 2009/03/26 22:26:08 petere Exp $
4+
* $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.120 2009/04/03 16:59:42 tgl Exp $
55
*
66
*********************************************************************
77
*/
@@ -1052,9 +1052,11 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
10521052
arg = Py_None;
10531053
}
10541054

1055-
if (PyList_SetItem(args, i, arg) == -1 ||
1056-
(proc->argnames &&
1057-
PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1))
1055+
if (PyList_SetItem(args, i, arg) == -1)
1056+
PLy_elog(ERROR, "PyList_SetItem() failed for PL/Python function \"%s\" while setting up arguments", proc->proname);
1057+
1058+
if (proc->argnames && proc->argnames[i] &&
1059+
PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
10581060
PLy_elog(ERROR, "PyDict_SetItemString() failed for PL/Python function \"%s\" while setting up arguments", proc->proname);
10591061
arg = NULL;
10601062
}
@@ -1081,7 +1083,8 @@ PLy_function_delete_args(PLyProcedure * proc)
10811083
return;
10821084

10831085
for (i = 0; i < proc->nargs; i++)
1084-
PyDict_DelItemString(proc->globals, proc->argnames[i]);
1086+
if (proc->argnames[i])
1087+
PyDict_DelItemString(proc->globals, proc->argnames[i]);
10851088
}
10861089

10871090

src/pl/plpython/sql/plpython_function.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,12 @@ $$ LANGUAGE plpythonu;
391391

392392

393393
--
394-
-- Test named parameters
394+
-- Test named and nameless parameters
395395
--
396+
CREATE FUNCTION test_param_names0(integer, integer) RETURNS int AS $$
397+
return args[0] + args[1]
398+
$$ LANGUAGE plpythonu;
399+
396400
CREATE FUNCTION test_param_names1(a0 integer, a1 text) RETURNS boolean AS $$
397401
assert a0 == args[0]
398402
assert a1 == args[1]

src/pl/plpython/sql/plpython_test.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ SELECT test_void_func1(), test_void_func1() IS NULL AS "is null";
7474
SELECT test_void_func2(); -- should fail
7575
SELECT test_return_none(), test_return_none() IS NULL AS "is null";
7676

77-
-- Test for functions with named parameters
77+
-- Test for functions with named and nameless parameters
78+
SELECT test_param_names0(2,7);
7879
SELECT test_param_names1(1,'text');
7980
SELECT test_param_names2(users) from users;
8081
SELECT test_param_names3(1);

0 commit comments

Comments
 (0)