]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
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:38:19 +0000 (18:38 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 8 Feb 2007 18:38:19 +0000 (18:38 +0000)
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

index 102b94b7894b839aedbdd491fca46c640a30927b..8cba7101a3789f5302be8098d564e1557a2f7dd8 100644 (file)
@@ -3,7 +3,7 @@
  *                       procedural language
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.93.2.2 2005/06/20 20:44:57 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.93.2.3 2007/02/08 18:38:19 tgl Exp $
  *
  *       This software is copyrighted by Jan Wieck - Hamburg.
  *
@@ -799,29 +799,29 @@ exec_stmt_block(PLpgSQL_execstate * estate, PLpgSQL_stmt_block * block)
                                {
                                        PLpgSQL_var *var = (PLpgSQL_var *) (estate->datums[n]);
 
+                                       /* free any old value, in case re-entering block */
                                        if (var->freeval)
                                        {
                                                pfree((void *) (var->value));
                                                var->freeval = false;
                                        }
 
-                                       if (!var->isconst || var->isnull)
+                                       /* Initially it contains a NULL */
+                                       var->value = (Datum) 0;
+                                       var->isnull = true;
+
+                                       if (var->default_val == NULL)
+                                       {
+                                               if (var->notnull)
+                                                       ereport(ERROR,
+                                                                       (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+                                                                        errmsg("variable \"%s\" declared NOT NULL cannot default to NULL",
+                                                                                       var->refname)));
+                                       }
+                                       else
                                        {
-                                               if (var->default_val == NULL)
-                                               {
-                                                       var->value = (Datum) 0;
-                                                       var->isnull = true;
-                                                       if (var->notnull)
-                                                               ereport(ERROR,
-                                                               (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
-                                                                errmsg("variable \"%s\" declared NOT NULL cannot default to NULL",
-                                                                               var->refname)));
-                                               }
-                                               else
-                                               {
-                                                       exec_assign_expr(estate, (PLpgSQL_datum *) var,
-                                                                                        var->default_val);
-                                               }
+                                               exec_assign_expr(estate, (PLpgSQL_datum *) var,
+                                                                                var->default_val);
                                        }
                                }
                                break;