From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22658 invoked by alias); 2 Jun 2015 08:56:33 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 35358 Received: (qmail 23164 invoked from network); 2 Jun 2015 08:56:26 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS autolearn=ham autolearn_force=no version=3.4.0 X-AuditID: cbfec7f4-f79c56d0000012ee-eb-556d6fb595a2 Date: Tue, 02 Jun 2015 09:56:00 +0100 From: Peter Stephenson To: zsh-workers@zsh.org Subject: Re: Arith expansion accepts extra closing parenthesis Message-id: <20150602095600.64d8ce61@pwslap01u.europe.root.pri> In-reply-to: <556C890E.80506@inlv.org> References: <556C890E.80506@inlv.org> Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrILMWRmVeSWpSXmKPExsVy+t/xK7pb83NDDbobbC0ONj9kcmD0WHXw A1MAYxSXTUpqTmZZapG+XQJXRscx1oLbvBX7v89namC8zNXFyMkhIWAisf/eQiYIW0ziwr31 bCC2kMBSRonZc926GLmA7BlMEtdW3WKHcLYySky/+p61i5GDg0VAVWLnzFSQBjYBQ4mpm2Yz gtgiAuISZ9eeZwGxhQXsJD5cXcMOYvMK2EssONbKCmJzCqhJHLqymwVimapE05mbYIv5BfQl rv79BHWQvcTMK2cYIXoFJX5MvgdWzyygJbF5WxMrhC0vsXnNW2aIOeoSN+7uZp/AKDQLScss JC2zkLQsYGRexSiaWppcUJyUnmuoV5yYW1yal66XnJ+7iRESsF92MC4+ZnWIUYCDUYmHl+FT TqgQa2JZcWXuIUYJDmYlEd5FybmhQrwpiZVVqUX58UWlOanFhxilOViUxHnn7nofIiSQnliS mp2aWpBaBJNl4uCUamD03rgtoUHy9YeEiXHKJ2UruDsU+bZsbpiStMP7rOhUK+YPfSVzVCau T2n4znZ2+34dRRX1D1/VbHUCEnsiLs32e/S98eurq5aHGr3Yjl6rWPnxygrOT/+ueS1xmaVV 8WRapcwzH+b2Ffs7Lj7ouskdJaLJsti+qnBRzW7BvR7F8v6sm+LfLytUYinOSDTUYi4qTgQA 2QviglQCAAA= On Mon, 1 Jun 2015 18:32:14 +0200 Martijn Dekker 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 ')'