Skip to content

Commit d7083cc

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 be8801e commit d7083cc

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
@@ -1129,8 +1129,9 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
11291129
{
11301130
/*
11311131
* Initialize the magic SQLSTATE and SQLERRM variables for
1132-
* the exception block. We needn't do this until we have
1133-
* found a matching exception.
1132+
* the exception block; this also frees values from any
1133+
* prior use of the same exception. We needn't do this
1134+
* until we have found a matching exception.
11341135
*/
11351136
PLpgSQL_var *state_var;
11361137
PLpgSQL_var *errm_var;
@@ -1154,13 +1155,6 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
11541155

11551156
rc = exec_stmts(estate, exception->action);
11561157

1157-
free_var(state_var);
1158-
state_var->value = (Datum) 0;
1159-
state_var->isnull = true;
1160-
free_var(errm_var);
1161-
errm_var->value = (Datum) 0;
1162-
errm_var->isnull = true;
1163-
11641158
break;
11651159
}
11661160
}

src/test/regress/expected/plpgsql.out

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

26472647
(1 row)
26482648

2649+
create function excpt_test4() returns text as $$
2650+
begin
2651+
begin perform 1/0;
2652+
exception when others then return sqlerrm; end;
2653+
end; $$ language plpgsql;
2654+
select excpt_test4();
2655+
excpt_test4
2656+
------------------
2657+
division by zero
2658+
(1 row)
2659+
26492660
drop function excpt_test1();
26502661
drop function excpt_test2();
26512662
drop function excpt_test3();
2663+
drop function excpt_test4();
26522664
-- parameters of raise stmt can be expressions
26532665
create function raise_exprs() returns void as $$
26542666
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)