Skip to content

Commit

Permalink
Fix here-document crash/hangup on Solaris/illumos in interactive shel…
Browse files Browse the repository at this point in the history
…ls (#721)

This was first reported in att#1472.
Attempting to cancel a heredoc with ^C or ^D can cause ksh to crash
with a segfault, or hang up and fill /tmp with files. Copy of the
reproducer:
   $ cat << EOS
   > <Press Ctrl+C or Ctrl+D>

src/cmd/ksh93/sh/main.c:
- Reset the lexer state in an interactive shell if here-document
  creation was cancelled. This patch has been adapted from Solaris:
  https://github.com/oracle/solaris-userland/blob/e478b48/components/ksh93/patches/400-29444429.patch
  • Loading branch information
JohnoKing authored and McDutchie committed Feb 23, 2024
1 parent 20fd6a4 commit 2f677ca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.

- Fixed some integer overflows that could occur when using TMOUT.

- Fixed a crash on Solaris and illumos when cancelling a here-document
with ^C or ^D in an interactive shell.

2024-02-17:

- Fixed a crash that could occur when using 'typeset -T' typed variables
Expand Down
14 changes: 14 additions & 0 deletions src/cmd/ksh93/sh/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,20 @@ static void exfile(Sfio_t *iop,int fno)
while(top=sfstack(iop,SF_POPSTACK))
sfclose(top);
}
/*
* Reset the lexer state and make sure the heredocs file is
* closed and set to NULL. For now we only do this when we get
* here in an interactive shell and we have a leftover heredoc.
*/
if(sh_isstate(SH_INTERACTIVE) && jmpval==SH_JMPERREXIT && sh.heredocs)
{
Lex_t *lp;
sfclose(sh.heredocs);
sh.heredocs = NULL;
lp = (Lex_t*)sh.lex_context;
lp->heredoc = NULL;
sh_lexopen(lp,0);
}
/* make sure that we own the terminal */
tcsetpgrp(job.fd,sh.pid);
}
Expand Down
16 changes: 16 additions & 0 deletions src/cmd/ksh93/tests/pty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1286,5 +1286,21 @@ c \Ek
r ^:prompt: "\$SHELL" -o vi -c 'read -s "foo\?:prompt: "'$
!

tst $LINENO <<"!"
L crash when attempting to cancel a heredoc in an interactive shell
# https://github.com/ksh93/ksh/pull/721
d 40
p :test-1:
w "$SHELL"
p :test-2:
w cat << EOS
p :test-3:
w \cD
p :test-4:
w print Exit status $?
u ^Exit status 0\r\n$
!

# ======
exit $((Errors<125?Errors:125))

0 comments on commit 2f677ca

Please sign in to comment.