[PATCH] parser: Only reject non-word tokens in case pattern

Herbert Xu <[email protected]> Sun, 12 Apr 2026 17:45:05 +0800
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
Nicolas Cavallari <[email protected]> wrote:
> This syntax is accepted by all shells except dash 0.5.13.2:
> 
> case "$test" in
> in) echo input;;
> out) echo output;;
> do) echo start;;
> done) echo stop;;
> esac
> 
> on dash 0.5.13.2, it fails with "Syntax error: "in" unexpected (expecting word)".
> Reverting e8c0324af64c("parser: Catch bogus case patterns") fixes it.
> 
> POSIX says that only esac should be recognized as a keyword when parsing the 
> first pattern of a case.
> 
> An example of a real script broken by this change:
> 
> https://salsa.debian.org/debian/netplug/-/blob/debian/unstable/scripts/netplug?ref_type=heads

Thanks for the report.  This patch should fix the problem:

---8<---
Allow keywords to be used as case patterns and only reject non-word
tokens.

Reported-by: Nicolas Cavallari <[email protected]>
Fixes: e8c0324af64c ("parser: Catch bogus case patterns")
Signed-off-by: Herbert Xu <[email protected]>

diff --git a/src/parser.c b/src/parser.c
index bbee1e3..bea4148 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -442,7 +442,7 @@ next_case:
 			cp->type = NCLIST;
 			app = &cp->nclist.pattern;
 			for (;;) {
-				if (lasttoken != TWORD)
+				if (lasttoken < TWORD)
 					synexpect(TWORD);
 				*app = ap = (union node *)stalloc(sizeof (struct narg));
 				ap->type = NARG;
-- 
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt