Closed posguy99 closed 3 months ago
Even simpler reproducer:
$ for(())
-ksh: syntax error: `{' unmatched
-ksh: syntax error: `{' unmatched
-ksh: syntax error: `{' unmatched
-ksh: syntax error: `{' unmatched
Continues forever.
Thanks for the report. On my end, ksh93u+ and ksh2020 are also broken, just not in the same way.
The current infinite-loop manifestation of the bug was introduced in: f8f2c4b60835dd5d37a02f8dc0fe62ab74e4da94 (found by trying old commits)
So, after some debugging and backtracing, I've found that what happens is this:
;
I can think of two strategies to fix this:
Strategy 2 might be the best way. It just so happens that sh_lexskip sets its own flag, lp->lexd.noarg, that we can use in sh_lex() to tell if it was called from sh_lexskip(). So we could use that to prevent it from throwing the syntax error and just return instead, handing control back to arithfor(), so that it can throw the syntax error after restoring state.
Here is a patch that does that. Please test.
diff --git a/src/cmd/ksh93/sh/lex.c b/src/cmd/ksh93/sh/lex.c
index ed8ed2db6..d60cb1cb9 100644
--- a/src/cmd/ksh93/sh/lex.c
+++ b/src/cmd/ksh93/sh/lex.c
@@ -344,7 +344,7 @@ int sh_lex(Lex_t* lp)
/* end-of-file */
if(mode==ST_BEGIN)
return lp->token=EOFSYM;
- if(mode >ST_NORM && lp->lexd.level>0)
+ if(mode >ST_NORM && lp->lexd.level>0 && !lp->lexd.noarg)
{
switch(c=endchar(lp))
{
That stopped the infinite loop.
$ for ((i = 5 , i == 0, --i))
arch/darwin.arm64-64/bin/ksh: syntax error: `))' unexpected
$ for (())
arch/darwin.arm64-64/bin/ksh: syntax error: `))' unexpected
$ echo $KSH_VERSION
Version AJM 93u+m/1.1.0-alpha+b75df92a/MOD 2024-08-26
Works for me.
A completely errant paste that wasn't meant for the terminal window exposed this...
Reproducer:
Continues forever until you kill the shell.
ksh93u+
does not have the problem:Obviously bad code shouldn't cause the shell to get stuck.