From 6575903d1da378e65e4444483d46664b81ef34e6 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Fri, 4 Sep 2020 04:54:35 +0200 Subject: [PATCH] Fix ${!} and ${$} throwing syntax error in here-document The inclusion of the special parameter expansions ${!} and ${$} (including the braces) in a here-document caused a syntax error. Bug reported by @Saikiran-m on Github. src/cmd/ksh93/data/lexstates.c: sh_lexstate7[]: - Change the state for ! (33) and $ (36) from S_ERR to 0. State table 7 is for skipping over ${...}, so this avoids the S_ERR state being invoked in sh_lex() (lex.c) for these characters while skipping ${...} in a here-doc. src/cmd/ksh93/tests/heredoc.sh: - Test evaluating the braces expansion form for all special parameters (@ * # ! $ - ? 0) in a here-document. Fixes: https://github.com/ksh93/ksh/issues/127 --- src/cmd/ksh93/data/lexstates.c | 2 +- src/cmd/ksh93/tests/heredoc.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cmd/ksh93/data/lexstates.c b/src/cmd/ksh93/data/lexstates.c index b06d8ca43..4f257a38c 100644 --- a/src/cmd/ksh93/data/lexstates.c +++ b/src/cmd/ksh93/data/lexstates.c @@ -274,7 +274,7 @@ static const char sh_lexstate7[256] = S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, - S_ERR, S_ERR, S_ERR, S_MOD2, S_ERR, S_MOD2, S_ERR, S_ERR, + S_ERR, 0, S_ERR, S_MOD2, 0, S_MOD2, S_ERR, S_ERR, S_ERR, S_ERR, S_MOD1, S_MOD1, S_ERR, S_MOD1, S_DOT, S_MOD2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, S_MOD1, S_ERR, S_ERR, S_MOD1, S_ERR, S_MOD1, diff --git a/src/cmd/ksh93/tests/heredoc.sh b/src/cmd/ksh93/tests/heredoc.sh index d6519f85c..726870400 100755 --- a/src/cmd/ksh93/tests/heredoc.sh +++ b/src/cmd/ksh93/tests/heredoc.sh @@ -509,5 +509,15 @@ EOF' || err_exit 'placing a command substitution and here-doc on the same line c $SHELL -c '$(true << !) !' 2> /dev/null && err_exit "a here-doc that isn't completed before the closing ) in a command substitution doesn't cause an error" +# ====== +# Check that ${p}, where p is a special parameter, does not cause a syntax error in a here-document. +# Bug for ${!} and ${$} reported at: https://github.com/ksh93/ksh/issues/127 +for p in @ \* \# ! \$ - \? 0; do + err=$(eval ': <&1) || err_exit "special parameter \${$p} throws syntax error in here-document (got \"$err\")" +done + # ====== exit $((Errors<125?Errors:125))