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

Re: Arith expansion accepts extra closing parenthesis



On Mon, 1 Jun 2015 18:32:14 +0200
Martijn Dekker <martijn@xxxxxxxx> wrote:
> I just found some more arithmetic parsing strangeness. zsh 5.0.8 and
> 4.3.11 both happily accept and ignore one (1) extra closing parenthesis
> in variable expansion within arithmetic expansion (even in POSIX mode).
> 
> % X='1)'
> % echo $(($X))
> 1
> % echo $((X))
> 1

I think this is a special case, down to the way we handle parenthesesed
expressions --- we treat it as if it's a top-level expression but
expect a closing parenthesis next.  So checking for a stray closing
parenthesis on return from the top level of parsing ought to be a robust
fix.

pws

diff --git a/Src/math.c b/Src/math.c
index 97a97b3..e20a90c 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -407,6 +407,13 @@ mathevall(char *s, enum prec_type prec_tp, char **ep)
     stack[0].val.type = MN_INTEGER;
     stack[0].val.u.l = 0;
     mathparse(prec_tp == MPREC_TOP ? TOPPREC : ARGPREC);
+    /*
+     * Internally, we parse the contents of parentheses at top
+     * precedence... so we can return a parenthesis here if
+     * there are too many at the end.
+     */
+    if (mtok == M_OUTPAR && !errflag)
+	zerr("unexpected ')'");
     *ep = ptr;
     DPUTS(!errflag && sp > 0,
 	  "BUG: math: wallabies roaming too freely in outback");
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index d284e08..2d35ea6 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -395,3 +395,17 @@
 >6
 >7
 >120
+
+  foo="(1)"
+  print $((foo))
+  print $(($foo))
+  print $(((2)))
+  foo="3)"
+  (print $((foo))) 2>&1
+  (print $(($foo))) 2>&1
+1: Good and bad trailing parentheses
+>1
+>1
+>2
+>(eval):6: unexpected ')'
+>(eval):7: unexpected ')'



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