Skip to content

Commit 7ad33ce

Browse files
committed
Rearrange use of plpgsql_add_initdatums() so that only the parsing of a
DECLARE section needs to know about it. Formerly, everyplace besides DECLARE that created variables needed to do "plpgsql_add_initdatums(NULL)" to prevent those variables from being sucked up as part of a subsequent DECLARE block. This is obviously error-prone, and in fact the SQLSTATE/SQLERRM patch had failed to do it for those two variables, leading to the bug recently exhibited by Asif Ali Rehman: a DECLARE within an exception handler tried to reinitialize SQLERRM. Although the SQLSTATE/SQLERRM patch isn't in any pre-8.1 branches, and so I can't point to a demonstrable failure there, it seems wise to back-patch this into the older branches anyway, just to keep the logic similar to HEAD.
1 parent b577aa9 commit 7ad33ce

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/pl/plpgsql/src/gram.y

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.97 2007/02/01 19:10:29 momjian Exp $
12+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.98 2007/02/08 18:37:14 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -272,15 +272,13 @@ decl_sect : opt_block_label
272272
$$.label = $1;
273273
$$.n_initvars = 0;
274274
$$.initvarnos = NULL;
275-
plpgsql_add_initdatums(NULL);
276275
}
277276
| opt_block_label decl_start
278277
{
279278
plpgsql_ns_setlocal(false);
280279
$$.label = $1;
281280
$$.n_initvars = 0;
282281
$$.initvarnos = NULL;
283-
plpgsql_add_initdatums(NULL);
284282
}
285283
| opt_block_label decl_start decl_stmts
286284
{
@@ -289,12 +287,16 @@ decl_sect : opt_block_label
289287
$$.label = $3;
290288
else
291289
$$.label = $1;
290+
/* Remember variables declared in decl_stmts */
292291
$$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos));
293292
}
294293
;
295294

296295
decl_start : K_DECLARE
297296
{
297+
/* Forget any variables created before block */
298+
plpgsql_add_initdatums(NULL);
299+
/* Make variable names be local to block */
298300
plpgsql_ns_setlocal(true);
299301
}
300302
;
@@ -990,9 +992,6 @@ for_control :
990992
-1),
991993
true);
992994

993-
/* put the for-variable into the local block */
994-
plpgsql_add_initdatums(NULL);
995-
996995
new = palloc0(sizeof(PLpgSQL_stmt_fori));
997996
new->cmd_type = PLPGSQL_STMT_FORI;
998997
new->lineno = $1;

src/pl/plpgsql/src/pl_comp.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.111 2007/02/01 19:10:29 momjian Exp $
11+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.112 2007/02/08 18:37:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -666,11 +666,6 @@ do_compile(FunctionCallInfo fcinfo,
666666
true);
667667
function->found_varno = var->dno;
668668

669-
/*
670-
* Forget about the above created variables
671-
*/
672-
plpgsql_add_initdatums(NULL);
673-
674669
/*
675670
* Now parse the function's text
676671
*/
@@ -1893,11 +1888,17 @@ plpgsql_adddatum(PLpgSQL_datum *new)
18931888

18941889

18951890
/* ----------
1896-
* plpgsql_add_initdatums Put all datum entries created
1897-
* since the last call into the
1898-
* finishing code block so the
1899-
* block knows which variables to
1900-
* reinitialize when entered.
1891+
* plpgsql_add_initdatums Make an array of the datum numbers of
1892+
* all the simple VAR datums created since the last call
1893+
* to this function.
1894+
*
1895+
* If varnos is NULL, we just forget any datum entries created since the
1896+
* last call.
1897+
*
1898+
* This is used around a DECLARE section to create a list of the VARs
1899+
* that have to be initialized at block entry. Note that VARs can also
1900+
* be created elsewhere than DECLARE, eg by a FOR-loop, but it is then
1901+
* the responsibility of special-purpose code to initialize them.
19011902
* ----------
19021903
*/
19031904
int

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