From 69306662344da8172381d3a511f462dced389ff1 Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Thu, 9 Jul 2020 14:12:04 -0700 Subject: [PATCH] Fix a syntax error when ((...)) is combined with redirections (#68) This bugfix was backported from ksh93v- 2013-10-10-alpha. src/cmd/ksh93/sh/parse: item(): - The done label is placed after the 'inout' call for handling I/O redirections. This causes the command below to produce a syntax error because the '>' is not handled as a redirection operator after 'goto done': $ ((1+2)) > /dev/null /usr/bin/ksh: syntax error: `>' unexpected Moving the done label fixes the syntax error as 'inout' is now called to handle the redirection operator. src/cmd/ksh93/tests/arith.sh: - Add a simple regression test. --- NEWS | 3 +++ src/cmd/ksh93/sh/parse.c | 4 ++-- src/cmd/ksh93/tests/arith.sh | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index e97a6150f..7d26efbf8 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,9 @@ Any uppercase BUG_* names are modernish shell bug IDs. - Fixed a memory leak when restoring PATH when temporarily setting PATH for a command (e.g. PATH=/foo/bar command ...) or in a virtual subshell. +- Combining ((...)) with redirections no longer causes a syntax error + due to the parser handling '>' incorrectly. + 2020-07-07: - Four of the date formats accepted by 'printf %()T' have had their diff --git a/src/cmd/ksh93/sh/parse.c b/src/cmd/ksh93/sh/parse.c index 223a89637..e14e4a8ff 100644 --- a/src/cmd/ksh93/sh/parse.c +++ b/src/cmd/ksh93/sh/parse.c @@ -1367,13 +1367,13 @@ static Shnode_t *item(Lex_t *lexp,int flag) return(t); } sh_lex(lexp); - /* redirection(s) following a compound command */ +done: + /* redirection(s) following a compound command or arithmetic expression */ if(io=inout(lexp,io,0)) { t=makeparent(lexp,TSETIO,t); t->tre.treio=io; } -done: lexp->lasttok = savwdval; lexp->lastline = savline; return(t); diff --git a/src/cmd/ksh93/tests/arith.sh b/src/cmd/ksh93/tests/arith.sh index e1aa91532..bfa6f68f8 100755 --- a/src/cmd/ksh93/tests/arith.sh +++ b/src/cmd/ksh93/tests/arith.sh @@ -761,4 +761,9 @@ x=0x1.0000000000000000000000000000p+6 v=$(printf $'%.28a\n' 64) [[ $v == "$x" ]] || err_exit "'printf %.28a 64' failed -- expected '$x', got '$v'" +# ====== +# Redirections with ((...)) should not cause a syntax error +(eval '((1)) >/dev/null') 2>/dev/null || err_exit 'redirections with ((...)) yield a syntax error' + +# ====== exit $((Errors<125?Errors:125))