From 40a5c45b48554ee3cb0b0526b3557e377554ee91 Mon Sep 17 00:00:00 2001 From: atheik <14833674+atheik@users.noreply.github.com> Date: Fri, 20 May 2022 22:29:20 +0100 Subject: [PATCH] Allow double quotes within backtick comsub within double quotes The following reproducer causes a spurious syntax error: foo="`: "("`" The nested double quotes are not recognised correctly, causing a syntax error at the '('. Removing the outer double quotes (which are unnecessary) is a workaround, but it's still a bug as every other shell accepts this. This bug has been present since the original Bourne shell. src/cmd/ksh93/sh/lex.c: sh_lex(): case S_QUOTE: - If the current character is '"' and we're in a `...` command substitution (ingrave is true), then do not switch to the old mode but keep using the ST_QUOTE state table. Thanks to @JohnoKing for the report and to @atheik for the fix. Co-authored by: Martijn Dekker Resolves: https://github.com/ksh93/ksh/issues/352 --- NEWS | 6 ++++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/lex.c | 15 +++++++++++++-- src/cmd/ksh93/tests/quoting2.sh | 5 +++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index de32cb3d5..e79fe4712 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,12 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0 Any uppercase BUG_* names are modernish shell bug IDs. +2022-05-20: + +- Fixed an ancient bug that caused a spurious syntax error when using double + quotes within a `backtick` command substitution within double quotes. + Thanks to atheik for the analysis and the fix. + 2022-03-10: - Fixed another corner case bug in the 'test'/'[' command. diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 212fb8a8a..63515c6c3 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2022-03-10" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2022-05-20" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh/lex.c b/src/cmd/ksh93/sh/lex.c index c9971851f..969b930da 100644 --- a/src/cmd/ksh93/sh/lex.c +++ b/src/cmd/ksh93/sh/lex.c @@ -772,8 +772,19 @@ int sh_lex(Lex_t* lp) { if(sh.inlineno > lp->lastline) lp->lex.last_quote = c; - mode = oldmode(lp); - poplevel(lp); + /* + * At this point, we know that the previous skipping of characters was done + * according to the ST_QUOTE state table. We also know that the character that + * stopped the skipping is also the ending character for the current level. If + * that character was " and if we are in a `...` statement, then don't switch + * to the old mode, as we are actually at the innermost section of a "`"..."`" + * statement, which should be skipped again using the ST_QUOTE state table. + */ + if(c!='"' || !ingrave) + { + mode = oldmode(lp); + poplevel(lp); + } } else if(c=='"' && n==RBRACE) mode = ST_QNEST; diff --git a/src/cmd/ksh93/tests/quoting2.sh b/src/cmd/ksh93/tests/quoting2.sh index 81eca34fe..59ab3268d 100755 --- a/src/cmd/ksh93/tests/quoting2.sh +++ b/src/cmd/ksh93/tests/quoting2.sh @@ -290,5 +290,10 @@ got="${var:+'}text between expansions${var:+'}" [[ $got == "$exp" ]] || err_exit "Single quotes misparsed in expansion operator string (8)" \ "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +# ====== +# https://github.com/ksh93/ksh/issues/352 +got=$(eval 'foo="`: "^Exec(\[[^]=]*])?="`"' 2>&1) || err_exit "Backtick command substitutions can't nest double quotes" \ + "(got $(printf %q "$got"))" + # ====== exit $((Errors<125?Errors:125))