Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCHes: fix reference checks in bin_typeset



The checks in bin_typeset report bogus self references in the two following examples:

typeset -n ref1
typeset -n ref2=ref1
() {
  typeset ref2=no-a-ref
  typeset -n ref1=ref2
}

Output:
(anon):typeset:2: ref1: invalid self reference

typeset ref=not-a-ref
() {
  typeset -n ref=ref
  () {
    typeset -n ref=ref
    echo $ref
  }
}

Output:
(anon):typeset:1: ref: invalid self reference
not-a-ref

The first patch eliminates these bogus warnings.

The following example triggers an invalid reference warning:

() {
  private -n ref1
  typeset -n ref2=ref1
}

Output:
(anon):typeset:2: ref1: invalid reference

The checks in bin_typeset fail to report the same warning for the following example:

typeset ref1=foo
() {
  private -n ref1
  typeset -n ref2=ref1
}

Output:
<none>

The second patch, built on top of the first one, fixes that.

Philippe


diff --git a/Src/builtin.c b/Src/builtin.c
index 5563bdba9..503d6db65 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3135,10 +3135,6 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func)
 		    zwarnnam(name, "%s: invalid reference", pm->node.nam);
 		    returnval = 1;
 		    continue;
-		} else if (pm->u.str && strcmp(pm->u.str, asg->name) == 0) {
-		    zwarnnam(name, "%s: invalid self reference", asg->name);
-		    returnval = 1;
-		    continue;
 		}
 	    }
 	    if (hn) {
diff --git a/Test/K01nameref.ztst b/Test/K01nameref.ztst
index 54f0aaf68..6328935e2 100644
--- a/Test/K01nameref.ztst
+++ b/Test/K01nameref.ztst
@@ -450,6 +450,25 @@ F:unexpected side-effects of previous tests
 1:looping assignment not allowed
 *?*invalid self reference
 
+ typeset ptr1=not-a-ref
+ () {
+   typeset -n ptr1=ptr1
+   () {
+     typeset -n ptr1=ptr1
+     echo $ptr1
+   }
+ }
+0:regression: not a self reference (test 1)
+>not-a-ref
+
+ typeset -n ptr1
+ typeset -n ptr2=ptr1
+ () {
+   typeset ptr2=no-a-ref
+   typeset -n ptr1=ptr2
+ }
+0:regression: not a self reference (test 2)
+
  unset -n ptr2
  typeset -n ptr2='path[2]'
  print -r -- $ptr2
diff --git a/Src/builtin.c b/Src/builtin.c
index 503d6db65..a39e7f33b 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3129,7 +3129,7 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func)
 
 	if (on & PM_NAMEREF) {
 	    if (asg->value.scalar &&
-		((pm = (Param)resolve_nameref((Param)hn, asg)) &&
+		((pm = (Param)paramtab->getnode(realparamtab, asg->value.scalar)) &&
 		 (pm->node.flags & PM_NAMEREF))) {
 		if (pm->node.flags & PM_SPECIAL) {
 		    zwarnnam(name, "%s: invalid reference", pm->node.nam);
diff --git a/Test/V10private.ztst b/Test/V10private.ztst
index 26004a2dc..4abbbf5c9 100644
--- a/Test/V10private.ztst
+++ b/Test/V10private.ztst
@@ -429,14 +429,7 @@ F:Here ptr1 points to global ptr2 so assignment succeeds
    setopt localoptions errreturn
    private -n ptr2
    typeset -n ptr1=ptr2
-   typeset -p ptr1 ptr2
-   typeset val=LOCAL
-   () {
-     ptr1=val
-     typeset -n
-     printf "v %s=%s\n" ptr1 "$ptr1" ptr2 "$ptr2"
-   }
-   typeset -p ptr1 ptr2
+   echo NOT REACHED
  }
  typeset -p ptr1 ptr2
 1:up-reference for private namerefs, end is in scope but private
@@ -445,6 +438,20 @@ F:Should we allow "public" namerefs to private parameters?
 *?*no such variable: ptr1
 *?*no such variable: ptr2
 
+ () {
+   typeset ptr2=foo
+   typeset -n ptr1=ptr2
+   () {
+     setopt localoptions errreturn
+     private -n ptr2
+     typeset -n ptr1=ptr2
+     echo NOT REACHED
+   }
+ }
+1:regression test for invalid reference detection
+F:Should we allow "public" namerefs to private parameters?
+*?*ptr2: invalid reference
+
  () {
    private x=1
    unset x


Messages sorted by: Reverse Date, Date, Thread, Author