Comments (5)
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.
from ksh.
Even simpler reproducer:
$ for(())
-ksh: syntax error: `{' unmatched
-ksh: syntax error: `{' unmatched
-ksh: syntax error: `{' unmatched
-ksh: syntax error: `{' unmatched
Continues forever.
from ksh.
Thanks for the report. On my end, ksh93u+ and ksh2020 are also broken, just not in the same way.
from ksh.
The current infinite-loop manifestation of the bug was introduced in: f8f2c4b (found by trying old commits)
from ksh.
So, after some debugging and backtracing, I've found that what happens is this:
- arithfor() in parse.c is called to parse the arithmetic for loop
- arithfor() saves current input stream and opens a new input stream to separate each of the three expressions in (( ... ))
- on line 758, arithfor() calls sh_lexskip() to find the next
;
- in lex.c line 1749, sh_lexskip() calls the main lexer function, sh_lex()
- in lex.c line 375, sh_lex() calls sh_syntax() to throw the syntax error in the reproducer
- sh_syntax() calls errormsg() from libast, which causes a longjmp straight back to the prompt, without giving arithfor() a chance to restore the input stream that it saved in step 2. This is the bug. We can get all sorts of unpredictable/undefined behaviour that way.
I can think of two strategies to fix this:
- Push context and sigsetjmp in arithfor() before calling sh_lexskip(), so that it longjmps back to arithfor() and it can restore the input stream before doing its own longjmp. This may carry a minor performance hit for nested arithmetic for loops.
- Since arithfor() throws a syntax error itself when needed anyway (and does so after restoring the input stream), we could also find a way for sh_lex() not to throw that syntax error when called from sh_lexskip.
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))
{
from ksh.
Related Issues (20)
- ksh93's printf(1) builtin somehow turns "\x20" (space character in UTF-8) into garbage HOT 10
- Syntax error in arithmetic command crashes ksh HOT 11
- ksh dev@3b33b438 fails signal regression test on macOS 13.6.7 Intel and ARM HOT 3
- [question] should "printf -v%s x" print "-vx" ? HOT 1
- Division by negative numbers returns "division by zero" HOT 10
- Shell arithmetic is broken for integer values beyond the system's maximum float precision HOT 5
- ksh dev@4467eaff does not build on macOS Intel/ARM 13.6.8 HOT 8
- command substitution here-doc syntax error not entered in history HOT 1
- Issue with ksh93 on OpenBSD HOT 16
- Migrate code to C99 HOT 1
- Build failures: musl and glibc-2.33 HOT 2
- jobs started inside a loop have the full loop text as their command line HOT 2
- Assignment of -0 in arithmetic expansion doesn't remove negative sign HOT 1
- Setting TMOUT in an optimized-away subshell removes the variable's special meaning
- TMOUT applies to 'read' from a non-terminal
- Regression: Double redirection in command substitution causes shell hang
- Update html docs to include documentation about the newer features
- SHOPT_EDPREDICT is broken & no def for CURSOR_UP, ERASE_EOS and pr_string
- Regression: ksh segfaults when unsetting SHLVL then exec'ing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ksh.