Skip to content

Commit 4c5b570

Browse files
committed
some daily fix on 1.7 branch
1 parent 207744a commit 4c5b570

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

expected/plpgsql_check_active.out

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,9 +3610,10 @@ select * from plpgsql_check_function('dyn_sql_1', security_warnings := true, fat
36103610
Detail: The EXECUTE expression is SQL injection vulnerable.
36113611
Hint: Use quote_ident, quote_literal or format function to secure variable.
36123612
warning:00000:13:EXECUTE:values passed to EXECUTE statement by USING clause was not used
3613-
error:XX000:14:EXECUTE:there is no parameter $1
3614-
Context: SQL statement "select $1"
3615-
(13 rows)
3613+
error:42P02:14:EXECUTE:there is no parameter $1
3614+
Query: select $1
3615+
-- ^
3616+
(14 rows)
36163617

36173618
drop function dyn_sql_1();
36183619
create type tp as (a int, b int);
@@ -3644,3 +3645,34 @@ select * from plpgsql_check_function('dyn_sql_2', security_warnings := true);
36443645

36453646
drop function dyn_sql_2();
36463647
drop type tp;
3648+
/*
3649+
* Should not to work
3650+
*/
3651+
create or replace function dyn_sql_2()
3652+
returns void as $$
3653+
declare
3654+
r record;
3655+
result int;
3656+
begin
3657+
select 10 a, 20 b into r;
3658+
raise notice '%', r.a;
3659+
execute 'select $1.a + $1.b' into result using r;
3660+
raise notice '%', result;
3661+
end;
3662+
$$ language plpgsql;
3663+
select dyn_sql_2(); --should to fail
3664+
NOTICE: 10
3665+
ERROR: could not identify column "a" in record data type
3666+
LINE 1: select $1.a + $1.b
3667+
^
3668+
QUERY: select $1.a + $1.b
3669+
CONTEXT: PL/pgSQL function dyn_sql_2() line 8 at EXECUTE
3670+
select * from plpgsql_check_function('dyn_sql_2', security_warnings := true);
3671+
plpgsql_check_function
3672+
-------------------------------------------------------------------------
3673+
error:42703:8:EXECUTE:could not identify column "a" in record data type
3674+
Query: select $1.a + $1.b
3675+
-- ^
3676+
(3 rows)
3677+
3678+
drop function dyn_sql_2();

sql/plpgsql_check_active.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,3 +2645,24 @@ select * from plpgsql_check_function('dyn_sql_2', security_warnings := true);
26452645
drop function dyn_sql_2();
26462646

26472647
drop type tp;
2648+
2649+
/*
2650+
* Should not to work
2651+
*/
2652+
create or replace function dyn_sql_2()
2653+
returns void as $$
2654+
declare
2655+
r record;
2656+
result int;
2657+
begin
2658+
select 10 a, 20 b into r;
2659+
raise notice '%', r.a;
2660+
execute 'select $1.a + $1.b' into result using r;
2661+
raise notice '%', result;
2662+
end;
2663+
$$ language plpgsql;
2664+
2665+
select dyn_sql_2(); --should to fail
2666+
select * from plpgsql_check_function('dyn_sql_2', security_warnings := true);
2667+
2668+
drop function dyn_sql_2();

src/check_expr.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,14 @@ plpgsql_check_expr_get_string(PLpgSQL_checkstate *cstate, PLpgSQL_expr *expr, bo
462462
Const *c;
463463

464464
c = expr_get_const(cstate, expr);
465-
*isnull = c && c->constisnull;
465+
if (c)
466+
{
467+
*isnull = c->constisnull;
468+
469+
return plpgsql_check_const_to_string(c);
470+
}
466471

467-
return plpgsql_check_const_to_string(c);
472+
return NULL;
468473
}
469474

470475
static void

src/stmtwalk.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "plpgsql_check.h"
1313

1414
#include "access/tupconvert.h"
15+
#include "catalog/pg_collation.h"
1516
#include "catalog/pg_type.h"
1617

1718
#include "nodes/nodeFuncs.h"
@@ -55,7 +56,10 @@ dynsql_param_ref(ParseState *pstate, ParamRef *pref)
5556
TupleDesc tupdesc;
5657

5758
if (pref->number < 1 || pref->number > nargs)
58-
elog(ERROR, "there is no parameter $%d", pref->number);
59+
ereport(ERROR,
60+
(errcode(ERRCODE_UNDEFINED_PARAMETER),
61+
errmsg("there is no parameter $%d", pref->number),
62+
parser_errposition(pstate, pref->location)));
5963

6064
expr = (PLpgSQL_expr *) list_nth(args, pref->number - 1);
6165

@@ -72,8 +76,12 @@ dynsql_param_ref(ParseState *pstate, ParamRef *pref)
7276
param->paramkind = PARAM_EXTERN;
7377
param->paramid = pref->number;
7478
param->paramtype = TupleDescAttr(tupdesc, 0)->atttypid;
75-
param->paramtypmod = TupleDescAttr(tupdesc, 0)->atttypmod;
76-
param->paramcollid = TupleDescAttr(tupdesc, 0)->attcollation;
79+
80+
/*
81+
* SPI_execute_with_args doesn't allow pass typmod.
82+
*/
83+
param->paramtypmod = -1;
84+
param->paramcollid = DEFAULT_COLLATION_OID;
7785

7886
ReleaseTupleDesc(tupdesc);
7987
}

0 commit comments

Comments
 (0)