Skip to contentSkip to conversation

Incorrect detection of EOF with interactive editline

PATCH In review Abdullah 2 participants 4 messages Sep 18, 2025 – Sep 22, 2025 1 message not in the archive

lore
Share & tools
lore

Abdullah

author
#0 lore raw

When an interactive shell (with editline) has a trap signal set (e.g., [1]) and the signal is caught AND there's nothing on the input line, the shell (or at least editline), after executing the trap handler, treats the empty line as EOF.

Try [1] with interactive editline, have a clean line input line, and change the size of your term, then your shell would die.

Also, we should call el_resize on WINCH, because we have not set the EL_SIGNAL option (and we shouldn't set EL_SIGNAL, that would mixup with trap handlers).

[1] $ trap 'echo "WINDOW CHANGED! New size:"; stty size' WINCH

author
#1 lore raw

PATCH 1/1 histedit: fix dash exiting when receiving a signal interactively

---
 src/input.c | 32 ++++++++++++++++++++++++++++----
 src/trap.c  | 16 +++++++++++++++-
 2 files changed, 43 insertions(+), 5 deletions(-)
src/input.c +28 -4
index c36d120..f775c9f 100644
@@ -39,6 +39,7 @@
 #include <string.h>
 #include <termios.h>
 #include <unistd.h>
+#include <wchar.h>
 
 /*
  * This file implements the input routines used by the parser.
@@ -276,6 +277,11 @@ preadfd(void)
 	int unget;
 	int pnr;
 	int nr;
+#ifndef SMALL
+	const LineInfoW *liw;
+	wchar_t wbuf[2];
+	const wchar_t *wc;
+#endif
 
 	nr = input_get_lleft(parsefile);
 
@@ -306,9 +312,8 @@ retry:
 			rl_cp = el_gets(el, &el_len);
 			popstackmark(&smark);
 		}
-		if (rl_cp == NULL)
-			nr = 0;
-		else {
+
+		if (rl_cp) {
 			if (nr > el_len)
 				nr = el_len;
 			memcpy(buf, rl_cp, nr);
@@ -317,9 +322,28 @@ retry:
 				rl_cp += nr;
 			} else
 				rl_cp = 0;
+			return nr;
 		}
 
-		return nr;
+		/* Got a signal while reading. */
+		if (el_len < 0 && errno == EINTR && pending_sig) {
+			out2c('\r');
+			liw = el_wline(el);
+			/* Don't lose the current line.
+			 * el_wpush() takes a L'\0' terminated string.
+			 * el_wline() gives us a string with N elements.
+			 * Simple solution: el_wpush() it wchar-by-wchar.
+			 */
+			wbuf[1] = L'\0';
+			wc = liw->buffer;
+			while (wc < liw->lastchar) {
+				wbuf[0] = *wc++;
+				el_wpush(el, wbuf);
+			}
+			dotrap();
+			goto retry;
+		}
+		return 0;
 	}
 #endif
 
src/trap.c +15 -1
index 23829a5..c045257 100644
@@ -53,6 +53,7 @@
 #include "error.h"
 #include "trap.h"
 #include "mystring.h"
+#include "myhistedit.h"
 
 /*
  * Sigmode records the current value of the signal handlers for the various
@@ -243,7 +244,12 @@ setsignal(int signo)
 		}
 	}
 
-	if (signo == SIGCHLD)
+	if (
+		signo == SIGCHLD
+#if defined(WINCH) && !defined(SMALL)
+		|| signo == WINCH
+#endif
+	)
 		action = S_CATCH;
 
 	t = &sigmode[signo - 1];
@@ -321,6 +327,11 @@ onsig(int signo)
 			return;
 	}
 
+#if defined(SIGWINCH) && !defined(SMALL)
+	if (signo == SIGWINCH && el)
+		el_resize(el);
+#endif
+
 	gotsig[signo - 1] = 1;
 	pending_sig = signo;
 
@@ -397,6 +408,9 @@ setinteractive(int on)
 	setsignal(SIGINT);
 	setsignal(SIGQUIT);
 	setsignal(SIGTERM);
+#if defined(WINCH) && !defined(SMALL)
+	setsignal(WINCH);
+#endif
 }
 
 
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH] histedit: fix dash exiting when receiving a signal interactively

Quoted 10 lines · Abdullah
src/input.c +9 -0
+               /* Got a signal while reading. */
+               if (el_len < 0 && errno == EINTR && pending_sig) {
+                       out2c('\r');
+                       liw = el_wline(el);
+                       /* Don't lose the current line.
+                        * el_wpush() takes a L'\0' terminated string.
+                        * el_wline() gives us a string with N elements.
+                        * Simple solution: el_wpush() it wchar-by-wchar.
+                        */

src/input.c retry: · line 346

Quoted 10 lines
src/input.c +10 -0
+                       wbuf[1] = L'\0';
+                       wc = liw->buffer;
+                       while (wc < liw->lastchar) {
+                               wbuf[0] = *wc++;
+                               el_wpush(el, wbuf);
+                       }
+                       dotrap();
+                       goto retry;
+               }
+               return 0;

Can you please explain why this is necessary and why el_gets can't handle it?

src/trap.c setsignal(int signo) · line 251

Quoted 6 lines
src/trap.c +5 -1
-       if (signo == SIGCHLD)
+       if (
+               signo == SIGCHLD
+#if defined(WINCH) && !defined(SMALL)
+               || signo == WINCH
+#endif

Please use SIGWINCH consistently.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Re: [PATCH] histedit: fix dash exiting when receiving a signal interactively

Quoted 8 lines · On Thu, Sep 18, 2025 at 10:45:38PM +0300, 3bd wrote:

EditLine has no "restore the current line to XYZ". No el_restore_line() or something similar.

We could avoid this if we set the SA_RESTART flag in sa_flags, then EditLine's internal call to read(2) won't fail. But I'm not sure about this. This could play with dash's internal handling of signals.

I think that's a defiency in libedit and should be addressed there.

Quoted 10 lines
Quoted 8 lines

src/trap.c setsignal(int signo) · line 251

Quoted 6 lines
src/trap.c +5 -1
-       if (signo == SIGCHLD)
+       if (
+               signo == SIGCHLD
+#if defined(WINCH) && !defined(SMALL)
+               || signo == WINCH
+#endif

Please use SIGWINCH consistently.

Oh, sorry, I just noticed the mistake.

I'm happy to take the SIGWINCH change though.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt