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

Commit 4651e37

Browse files
committed
Free SQLSTATE and SQLERRM no earlier than other PL/pgSQL variables.
"RETURN SQLERRM" prompted plpgsql_exec_function() to read from freed memory. Back-patch to 9.0 (all supported versions). Little code ran between the premature free and the read, so non-assert builds are unlikely to witness user-visible consequences.
1 parent f864fe0 commit 4651e37

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/pl/plpgsql/src/pl_exec.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,8 +1220,9 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
12201220
{
12211221
/*
12221222
* Initialize the magic SQLSTATE and SQLERRM variables for
1223-
* the exception block. We needn't do this until we have
1224-
* found a matching exception.
1223+
* the exception block; this also frees values from any
1224+
* prior use of the same exception. We needn't do this
1225+
* until we have found a matching exception.
12251226
*/
12261227
PLpgSQL_var *state_var;
12271228
PLpgSQL_var *errm_var;
@@ -1245,13 +1246,6 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
12451246

12461247
rc = exec_stmts(estate, exception->action);
12471248

1248-
free_var(state_var);
1249-
state_var->value = (Datum) 0;
1250-
state_var->isnull = true;
1251-
free_var(errm_var);
1252-
errm_var->value = (Datum) 0;
1253-
errm_var->isnull = true;
1254-
12551249
break;
12561250
}
12571251
}

src/test/regress/expected/plpgsql.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,9 +2644,21 @@ NOTICE: P0001 user exception
26442644

26452645
(1 row)
26462646

2647+
create function excpt_test4() returns text as $$
2648+
begin
2649+
begin perform 1/0;
2650+
exception when others then return sqlerrm; end;
2651+
end; $$ language plpgsql;
2652+
select excpt_test4();
2653+
excpt_test4
2654+
------------------
2655+
division by zero
2656+
(1 row)
2657+
26472658
drop function excpt_test1();
26482659
drop function excpt_test2();
26492660
drop function excpt_test3();
2661+
drop function excpt_test4();
26502662
-- parameters of raise stmt can be expressions
26512663
create function raise_exprs() returns void as $$
26522664
declare

src/test/regress/sql/plpgsql.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2241,11 +2241,19 @@ begin
22412241
raise notice '% %', sqlstate, sqlerrm;
22422242
end;
22432243
end; $$ language plpgsql;
2244-
22452244
select excpt_test3();
2245+
2246+
create function excpt_test4() returns text as $$
2247+
begin
2248+
begin perform 1/0;
2249+
exception when others then return sqlerrm; end;
2250+
end; $$ language plpgsql;
2251+
select excpt_test4();
2252+
22462253
drop function excpt_test1();
22472254
drop function excpt_test2();
22482255
drop function excpt_test3();
2256+
drop function excpt_test4();
22492257

22502258
-- parameters of raise stmt can be expressions
22512259
create function raise_exprs() returns void as $$

0 commit comments

Comments
 (0)