diff --git a/NEWS b/NEWS index 966a6714a265..969ffa625223 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/src/cmd/ksh93/sh/main.c b/src/cmd/ksh93/sh/main.c index 81cf986ef29f..01f3720d61bf 100644 --- a/src/cmd/ksh93/sh/main.c +++ b/src/cmd/ksh93/sh/main.c @@ -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); } diff --git a/src/cmd/ksh93/tests/pty.sh b/src/cmd/ksh93/tests/pty.sh index e504d3af23bc..ecea9d19ff3a 100755 --- a/src/cmd/ksh93/tests/pty.sh +++ b/src/cmd/ksh93/tests/pty.sh @@ -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))