1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Reset lexer state on syntax error and on SIGINT (Ctrl+C)

ksh crashed if you pressed Ctrl+C or Ctrl+D on a PS2 prompt while
you haven't finished entering a $(command substitution). It
corrupts subsequent command substitutions. Sometimes the situation
recovers, sometimes the shell crashes.

Simple crash reproducer:

  $ PS1="\$(echo foo) \$(echo bar) \$(echo baz) > "
  foo bar baz > echo $(                        <-- now press Ctrl+D
  > ksh: syntax error: `(' unmatched
  Memory fault

The same happens with Ctrl+C, minus the syntax error message.

The problem is that the lexer state becomes inconsistent when the
lexer is interrupted in the middle of reading a command
substitution of the form $( ... ). This is tracked in the
'lexd.dolparen' variable in the lexer state struct.

Resetting that variable is sufficient to fix this issue. However,
in this commit I prefer to just reinitialise the lexer state
completely to pre-empt any other possible issues. Whether there was
a syntax error or the user pressed Ctrl+C, we just interrupted all
lexing and parsing, so the lexer *should* restart from scratch.

src/cmd/ksh93/sh/fault.c: sh_fault():
- If the shell is in an interactive state (e.g. not a subshell) and
  SIGINT was received, reinitialise the lexer state. This fixes the
  crash with Ctrl+C.

src/cmd/ksh93/sh/lex.c: sh_syntax():
- When handling a syntax error, reset the lexer state. This fixes
  the crash with Ctrl+D.

NEWS:
- Also add the forgotten item for the previous fix (re: 2322f939).
This commit is contained in:
Martijn Dekker 2021-12-09 07:05:36 +01:00
parent 350e52877b
commit feedc05037
4 changed files with 16 additions and 1 deletions

View file

@ -68,6 +68,9 @@ void sh_fault(register int sig)
register char *trap;
register struct checkpt *pp = (struct checkpt*)shp->jmplist;
int action=0;
/* reset lexer state on Ctrl+C */
if(sh_isstate(SH_INTERACTIVE) && sig==SIGINT)
sh_lexopen(sh.lex_context, &sh, 0);
/* reset handler */
if(!(sig&SH_TRAP))
signal(sig, sh_fault);