Skip to content

Commit

Permalink
Fix infinite loop in emacs mode when HISTFILE is empty (#701)
Browse files Browse the repository at this point in the history
This commit fixes a regression introduced in 5eeeed0. Pressing the
up arrow in emacs mode when the history file is empty or otherwise
cannot be accessed causes an infinite loop that repeatedly calls
beep().

Reproducer (using /dev/null as HISTFILE also reproduces the bug):
echo '' > /tmp/a; ENV=/dev/null HISTFILE=/tmp/a ksh -o emacs <Press
Up Arrow>

The loop happens because when blanks lines in the history file are
skipped at L700, ed_ungetchar loops back to L635, beeps at L650,
and since ksh is compiled with ESH_NFIRST enabled by default a goto
at L655 jumps back to the code the initial ed_ungetchar is located.
Since the variable c is still ^P, this loop continues into
perpetuity. (Also of note is that if the history is not empty, but
the first command is a blank space, ksh will loop once that space
is reached while using the up arrow.)

src/cmd/ksh93/edit/emacs.c:
- Rather than use a goto that loops repeatedly, remove the ifndef
  that prevented use of the continue statement. Once the beep
  occurs we evidently can't go back any further in the command
  history, so bail out.
  • Loading branch information
JohnoKing authored and McDutchie committed Jan 14, 2024
1 parent 5e6c421 commit 7bfcd1e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ This documents significant changes in the 1.0 branch of ksh 93u+m.
For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.

2024-01-14:

- Fixed a regression introduced on 2022-11-01 that caused ksh to enter
an infinite loop after using the up arrow in emacs mode with an empty
history file.

2024-01-11:

- Fixed ancient incorrect behaviour in the 'kill' built-in: it sent SIGCONT
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/ksh93/edit/emacs.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,7 @@ int ed_emacsread(void *context, int fd,char *buff,int scend, int reedit)
{
hline = hismin+1;
beep();
#ifndef ESH_NFIRST
continue;
#endif
}
goto common;

Expand Down

0 comments on commit 7bfcd1e

Please sign in to comment.