summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNoah Misch <noah@leadboat.com>2015-02-25 23:48:28 -0500
committerNoah Misch <noah@leadboat.com>2015-02-25 23:48:54 -0500
commit4651e37cb1c9cc34643921bdc8b783d1c81b488f (patch)
tree7eea537b1b4a7cf4b93c292568c2f5f13f28ef52 /src
parentf864fe07403e6d6b38a06490c01a62a41872899a (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/pl/plpgsql/src/pl_exec.c12
-rw-r--r--src/test/regress/expected/plpgsql.out12
-rw-r--r--src/test/regress/sql/plpgsql.sql10
3 files changed, 24 insertions, 10 deletions
diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c
index 39866edca38..9601345c84a 100644
--- a/src/pl/plpgsql/src/pl_exec.c
+++ b/src/pl/plpgsql/src/pl_exec.c
@@ -1220,8 +1220,9 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
{
/*
* Initialize the magic SQLSTATE and SQLERRM variables for
- * the exception block. We needn't do this until we have
- * found a matching exception.
+ * the exception block; this also frees values from any
+ * prior use of the same exception. We needn't do this
+ * until we have found a matching exception.
*/
PLpgSQL_var *state_var;
PLpgSQL_var *errm_var;
@@ -1245,13 +1246,6 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
rc = exec_stmts(estate, exception->action);
- free_var(state_var);
- state_var->value = (Datum) 0;
- state_var->isnull = true;
- free_var(errm_var);
- errm_var->value = (Datum) 0;
- errm_var->isnull = true;
-
break;
}
}
diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expected/plpgsql.out
index 7feea0ac476..844f6fc82aa 100644
--- a/src/test/regress/expected/plpgsql.out
+++ b/src/test/regress/expected/plpgsql.out
@@ -2644,9 +2644,21 @@ NOTICE: P0001 user exception
(1 row)
+create function excpt_test4() returns text as $$
+begin
+ begin perform 1/0;
+ exception when others then return sqlerrm; end;
+end; $$ language plpgsql;
+select excpt_test4();
+ excpt_test4
+------------------
+ division by zero
+(1 row)
+
drop function excpt_test1();
drop function excpt_test2();
drop function excpt_test3();
+drop function excpt_test4();
-- parameters of raise stmt can be expressions
create function raise_exprs() returns void as $$
declare
diff --git a/src/test/regress/sql/plpgsql.sql b/src/test/regress/sql/plpgsql.sql
index 0f8465faa47..6b3264b7bc0 100644
--- a/src/test/regress/sql/plpgsql.sql
+++ b/src/test/regress/sql/plpgsql.sql
@@ -2241,11 +2241,19 @@ begin
raise notice '% %', sqlstate, sqlerrm;
end;
end; $$ language plpgsql;
-
select excpt_test3();
+
+create function excpt_test4() returns text as $$
+begin
+ begin perform 1/0;
+ exception when others then return sqlerrm; end;
+end; $$ language plpgsql;
+select excpt_test4();
+
drop function excpt_test1();
drop function excpt_test2();
drop function excpt_test3();
+drop function excpt_test4();
-- parameters of raise stmt can be expressions
create function raise_exprs() returns void as $$