zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk (Zsh hackers list)
Subject: PATCH: bug in WARN_CREATE_GLOBAL
Date: Fri, 25 Nov 2005 10:30:18 +0000	[thread overview]
Message-ID: <EXCHANGE03mvzUZvKkx0000defa@exchange03.csr.com> (raw)

This is a bug:

% fn() {
  setopt warncreateglobal
  PWD=~ echo
}
% fn
fn:2: scalar parameter PWD created globally in function

because PWD was already set.  The warning occurs because PWD was
temporarily removed from the parameter table to be restored after the
builtin finished.  The fix is to pass a special flag to addvars() if
that is happening.  I've taken the opportunity to tidy up the other
flags to addvars().

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.94
diff -u -r1.94 exec.c
--- Src/exec.c	13 Oct 2005 16:30:14 -0000	1.94
+++ Src/exec.c	25 Nov 2005 10:24:42 -0000
@@ -30,6 +30,17 @@
 #include "zsh.mdh"
 #include "exec.pro"
 
+/* Flags for last argument of addvars */
+
+enum {
+    /* Export the variable for "VAR=val cmd ..." */
+    ADDVAR_EXPORT =   1 << 0,
+    /* Apply restrictions for variable */
+    ADDVAR_RESTRICT = 1 << 1,
+    /* Variable list is being restored later */
+    ADDVAR_RESTORE =  1 << 2
+};
+
 /* used to suppress ERREXIT and trapping of SIGZERR, SIGEXIT. */
 
 /**/
@@ -1644,7 +1655,7 @@
 
 /**/
 static void
-addvars(Estate state, Wordcode pc, int export)
+addvars(Estate state, Wordcode pc, int addflags)
 {
     LinkList vl;
     int xtr, isstr, htok = 0;
@@ -1655,7 +1666,14 @@
     wordcode ac;
     local_list1(svl);
 
-    flags = (locallevel > 0 && isset(WARNCREATEGLOBAL)) ?
+    /*
+     * Warn when creating a global without using typeset -g in a
+     * function.  Don't do this if there is a list of variables marked
+     * to be restored after the command, since then the assignment
+     * is implicitly scoped.
+     */
+    flags = (!(addflags & ADDVAR_RESTORE) &&
+	     locallevel > 0 && isset(WARNCREATEGLOBAL)) ?
 	ASSPM_WARN_CREATE : 0;
     xtr = isset(XTRACE);
     if (xtr) {
@@ -1708,8 +1726,8 @@
 		quotedzputs(val, xtrerr);
 		fputc(' ', xtrerr);
 	    }
-	    if (export && !strchr(name, '[')) {
-		if (export < 0 && isset(RESTRICTED) &&
+	    if ((addflags & ADDVAR_EXPORT) && !strchr(name, '[')) {
+		if ((addflags & ADDVAR_RESTRICT) && isset(RESTRICTED) &&
 		    (pm = (Param) paramtab->removenode(paramtab, name)) &&
 		    (pm->flags & PM_RESTRICTED)) {
 		    zerr("%s: restricted", pm->nam, 0);
@@ -2520,7 +2538,13 @@
 		/* Export this if the command is a shell function,
 		 * but not if it's a builtin.
 		 */
-		addvars(state, varspc, is_shfunc);
+		int flags = 0;
+		if (is_shfunc)
+		    flags |= ADDVAR_EXPORT;
+		if (restorelist)
+		    flags |= ADDVAR_RESTORE;
+
+		addvars(state, varspc, flags);
 		if (errflag) {
 		    if (restorelist)
 			restore_params(restorelist, removelist);
@@ -2597,7 +2621,7 @@
 	    }
 	    if (type == WC_SIMPLE) {
 		if (varspc) {
-		    addvars(state, varspc, -1);
+		    addvars(state, varspc, ADDVAR_EXPORT|ADDVAR_RESTRICT);
 		    if (errflag)
 			_exit(1);
 		}

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


                 reply	other threads:[~2005-11-25 10:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=EXCHANGE03mvzUZvKkx0000defa@exchange03.csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).