Fix an ancient logic error in plpgsql's exec_stmt_block: it thought it could
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 8 Feb 2007 18:37:55 +0000 (18:37 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 8 Feb 2007 18:37:55 +0000 (18:37 +0000)
commitd6e3ae48ef4822259b00fe5fabd9791a30c19387
treecc59950227a78e9be3051ee492e5c3a4cb4a84d2
parentcd0d50466f5e85d4a4a1df27abf228110075b2d5
Fix an ancient logic error in plpgsql's exec_stmt_block: it thought it could
get away with not (re)initializing a local variable if the variable is marked
"isconst" and not "isnull".  Unfortunately it makes this decision after having
already freed the old value, meaning that something like

   for i in 1..10 loop
     declare c constant text := 'hi there';

leads to subsequent accesses to freed memory, and hence probably crashes.
(In particular, this is why Asif Ali Rehman's bug leads to crash and not
just an unexpectedly-NULL value for SQLERRM: SQLERRM is marked CONSTANT
and so triggers this error.)

The whole thing seems wrong on its face anyway: CONSTANT means that you can't
change the variable inside the block, not that the initializer expression is
guaranteed not to change value across successive block entries.  Hence,
remove the "optimization" instead of trying to fix it.
src/pl/plpgsql/src/pl_exec.c