Skip to content

Commit 034d05d

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 f7b4190 commit 034d05d

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
@@ -1123,8 +1123,9 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
11231123
{
11241124
/*
11251125
* Initialize the magic SQLSTATE and SQLERRM variables for
1126-
* the exception block. We needn't do this until we have
1127-
* found a matching exception.
1126+
* the exception block; this also frees values from any
1127+
* prior use of the same exception. We needn't do this
1128+
* until we have found a matching exception.
11281129
*/
11291130
PLpgSQL_var *state_var;
11301131
PLpgSQL_var *errm_var;
@@ -1148,13 +1149,6 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
11481149

11491150
rc = exec_stmts(estate, exception->action);
11501151

1151-
free_var(state_var);
1152-
state_var->value = (Datum) 0;
1153-
state_var->isnull = true;
1154-
free_var(errm_var);
1155-
errm_var->value = (Datum) 0;
1156-
errm_var->isnull = true;
1157-
11581152
break;
11591153
}
11601154
}

src/test/regress/expected/plpgsql.out

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

24992499
(1 row)
25002500

2501+
create function excpt_test4() returns text as $$
2502+
begin
2503+
begin perform 1/0;
2504+
exception when others then return sqlerrm; end;
2505+
end; $$ language plpgsql;
2506+
select excpt_test4();
2507+
excpt_test4
2508+
------------------
2509+
division by zero
2510+
(1 row)
2511+
25012512
drop function excpt_test1();
25022513
drop function excpt_test2();
25032514
drop function excpt_test3();
2515+
drop function excpt_test4();
25042516
-- parameters of raise stmt can be expressions
25052517
create function raise_exprs() returns void as $$
25062518
declare

src/test/regress/sql/plpgsql.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,11 +2118,19 @@ begin
21182118
raise notice '% %', sqlstate, sqlerrm;
21192119
end;
21202120
end; $$ language plpgsql;
2121-
21222121
select excpt_test3();
2122+
2123+
create function excpt_test4() returns text as $$
2124+
begin
2125+
begin perform 1/0;
2126+
exception when others then return sqlerrm; end;
2127+
end; $$ language plpgsql;
2128+
select excpt_test4();
2129+
21232130
drop function excpt_test1();
21242131
drop function excpt_test2();
21252132
drop function excpt_test3();
2133+
drop function excpt_test4();
21262134

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy