Skip to content

Commit 0af2293

Browse files
committed
Fix loss of here-docs on ^C (re: cf53abb, dfb699b, 2e000c9)
Reproducer (requires interactive shell): $ function fn { cat < this is > a test > EOF > } $ fn this is a test $ sleep 3 ^C $ fn (no output) All here-documents in functions that have been loaded are lost when the user interrupts any command on the interactive shell, or when any other condition occurs that causes the shell to abort and return to the prompt (i.e., the SH_JMPERREXIT jmpval condition). The bug was caused by the Solaris patch in the referenced commit, which closes and resets `sh.heredocs`, the Sfio stream that keeps loaded here-docs, including those in memory-resident functions. Each here-doc becomes part of the stream and its offset and size is kept track of. So, calling sfclose() on that is clearly incorrect. The patch was meant to fix this bug (and did fix it): att#1472 In that bug, ksh crashed badly if the user interrupted the entry of a here-doc on the interactive command line, and then a command substitution was executed as part of the PS1 prompt. The issue was that, due to the interrupt, the 'heredoc' member of the Lex_t lexer state struct was never reset properly before the next prompt was expanded/printed, causing the command substitution to be wrongly treated as a here-doc. So, resetting lp->heredoc is the only correct and necessary part of commit cf53abb. However, in ksh 93u+m, this has been made unnecessary by the combination of commits dfb699b, which resets the lexer state before expanding and showing each PS1 prompt, and 2e000c9, which fixes sh_lexopen() to reset all of the lexer struct except $LINENO- related items, which fixed it to reset lp->heredoc as well. So, the referenced commit is not only broken but (as of 2e000c9) also obsolete, and is now reverted. The full discussion and proof of correctness of this fix is too elaborate for a commit message, but can be found at: #978 (comment) Resolves: #978
1 parent e1a5673 commit 0af2293

4 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎NEWS‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ This documents significant changes in the dev branch of ksh 93u+m.
22
For full details, see the git log at: https://github.com/ksh93/ksh
33
Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.
44

5+
2026-04-22:
6+
7+
- Fixed a bug that causes the loss of the contents of all here-documents
8+
in already-loaded functions if a command on the interactive shell is
9+
interrupted, such as with Ctrl+C. Bug introduced on 2024-02-22.
10+
511
2026-04-20:
612

713
- [v1.1] Removed an undocumented and inconsistent hack that made

‎src/cmd/ksh93/include/version.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include
2222
#include "git.h"
2323

24-
#define SH_RELEASE_DATE "2026-04-20" /* must be in this format for $((.sh.version)) */
24+
#define SH_RELEASE_DATE "2026-04-22" /* must be in this format for $((.sh.version)) */
2525
/*
2626
* This comment keeps SH_RELEASE_DATE a few lines away from SH_RELEASE_SVER to avoid
2727
* merge conflicts when cherry-picking dev branch commits onto a release branch.

‎src/cmd/ksh93/sh/main.c‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -428,20 +428,6 @@ static void exfile(Sfio_t *iop,int fno)
428428
while(top=sfstack(iop,SFIO_POPSTACK))
429429
sfclose(top);
430430
}
431-
/*
432-
* Reset the lexer state and make sure the heredocs file is
433-
* closed and set to NULL. For now we only do this when we get
434-
* here in an interactive shell and we have a leftover heredoc.
435-
*/
436-
if(sh_isstate(SH_INTERACTIVE) && jmpval==SH_JMPERREXIT && sh.heredocs)
437-
{
438-
Lex_t *lp;
439-
sfclose(sh.heredocs);
440-
sh.heredocs = NULL;
441-
lp = (Lex_t*)sh.lex_context;
442-
lp->heredoc = NULL;
443-
sh_lexopen(lp,0);
444-
}
445431
/* make sure that we own the terminal */
446432
tcsetpgrp(job.fd,sh.pid);
447433
}

‎src/cmd/ksh93/tests/pty.sh‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,5 +1413,22 @@ I print
14131413
r ^1\r\n$
14141414
!
14151415

1416+
tst $LINENO <<"!"
1417+
L loss of here-doc in loaded function after interrupting a here-doc
1418+
# https://github.com/ksh93/ksh/issues/978
1419+
1420+
d 15
1421+
P :test-1:
1422+
w function fn { cat <
1423+
P :test-2:
1424+
w sleep 3
1425+
s 50
1426+
c \cC
1427+
P :test-3:
1428+
w fn
1429+
r fn
1430+
r ^this is a test\r\n$
1431+
!
1432+
14161433
# ======
14171434
exit $((Errors<125?Errors:125))

0 commit comments

Comments
 (0)