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

Re: Arith parsing bug with minus after $#



On Thu, 28 May 2015 21:42:41 +0200
Martijn Dekker <martijn@xxxxxxxx> wrote:
> % set --
> % echo $#
> 0
> % echo $(($#-1))
> 41

That's not a compatibility issue, that's just plain weird.  I don't know
the POSIX terminology.

The problem is the overloading of "#" --- the test to establish what to
do with it is trying too hard to resolve to ${#-}, which is a valid
substitution, because it hasn't taken into account that there are no
braces.  So what you're seeing is ${#-}1.

"-" is overloaded, too, so there could be other cases involving those
two characters where they're misinterpreted, even with braces.

diff --git a/Src/subst.c b/Src/subst.c
index d4a04b8..168f7f1 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2170,7 +2170,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags)
 		     */
 		    || ((cc == '#' || cc == Pound) &&
 			s[2] == Outbrace)
-		    || cc == '-' || (cc == ':' && s[2] == '-')
+		    || (inbrace && (cc == '-' || (cc == ':' && s[2] == '-')))
 		    || (isstring(cc) && (s[2] == Inbrace || s[2] == Inpar)))) {
 	    getlen = 1 + whichlen, s++;
 	    /*
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index d96ffb6..c41e05e 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1703,3 +1703,8 @@
   funnychars='The qu*nk br!wan f@x j/mps o[]r \(e la~# ^"&;'
   [[ $funnychars = ${~${(b)funnychars}} ]]
 0:${(b)...} quoting protects from GLOB_SUBST
+
+  set --
+  print $#-1
+0:Avoid confusion after overloaded characters in braceless substitution
+>0-1



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