From b7f5aebc89e65d7a359171a97ed20ab9d58c33d4 Mon Sep 17 00:00:00 2001
From: Grisha Levit <grishalevit@gmail.com>
Date: Sun, 17 Aug 2025 00:19:50 -0400
Subject: [PATCH] do not modify _rl_iscxt after signal cleanup

Ref: https://lists.gnu.org/archive/html/bug-bash/2025-08/msg00080.html
---
 lib/readline/isearch.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/lib/readline/isearch.c b/lib/readline/isearch.c
index 7b845c20..241e2ee0 100644
--- a/lib/readline/isearch.c
+++ b/lib/readline/isearch.c
@@ -319,17 +319,24 @@ _rl_search_getchar (_rl_search_cxt *cxt)
 
   /* Read a key and decide how to proceed. */
   RL_SETSTATE(RL_STATE_MOREINPUT);
-  c = cxt->lastc = rl_read_key ();
+  c = rl_read_key ();
   RL_UNSETSTATE(RL_STATE_MOREINPUT);
 
+  if (RL_ISSTATE (RL_STATE_ISEARCH) == 0)	/* signal could turn it off */
+    return -1;
+
 #if defined (HANDLE_MULTIBYTE)
   /* This ends up with C (and LASTC) being set to the last byte of the
      multibyte character.  In most cases c == lastc == mb[0] */
   if (c >= 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0)
-    c = cxt->lastc = _rl_read_mbstring (cxt->lastc, cxt->mb, MB_LEN_MAX);
+    {
+      c = _rl_read_mbstring (c, cxt->mb, MB_LEN_MAX);
+      if (RL_ISSTATE (RL_STATE_ISEARCH) == 0)	/* signal could turn it off */
+	return -1;
+    }
 #endif
 
-  RL_CHECK_SIGNALS ();
+  cxt->lastc = c;
   return c;
 }
 
@@ -370,7 +377,12 @@ _rl_isearch_dispatch (_rl_search_cxt *cxt, int c)
 	  goto opcode_dispatch;	
         }
       else if (_rl_pushed_input_available ())	/* eat extra char we pushed back */
-	c = cxt->lastc = rl_read_key ();
+	{
+	  c = rl_read_key ();
+	  if (RL_ISSTATE (RL_STATE_ISEARCH) == 0)   /* signal could turn it off */
+	    return -1;
+	  cxt->lastc = c;
+	}
       else
 	c = cxt->lastc;			/* last ditch */
     }
@@ -700,11 +712,17 @@ opcode_dispatch:
 
     case -7:	/* bracketed paste */
       paste = _rl_bracketed_text (&pastelen);
+      if (RL_ISSTATE (RL_STATE_ISEARCH) == 0)	/* signal could turn it off */
+	{
+	  xfree (paste);
+	  return -1;
+        }
       if (paste == 0 || *paste == 0)
 	{
 	  xfree (paste);
 	  break;
 	}
+
       if (_rl_enable_active_region)
 	rl_activate_mark ();
       if (cxt->search_string_index + pastelen + 1 >= cxt->search_string_size)
@@ -729,6 +747,9 @@ opcode_dispatch:
 	_rl_restore_tty_signals ();
 #endif
 
+      if (RL_ISSTATE (RL_STATE_ISEARCH) == 0)	/* signal could turn it off */
+	return -1;
+
       if (c < 0)
 	{
 	  cxt->sflags |= SF_FAILED;
@@ -923,6 +944,10 @@ rl_search_history (int direction, int invoking_key)
   for (;;)
     {
       c = _rl_search_getchar (cxt);
+
+      if (RL_ISSTATE (RL_STATE_ISEARCH) == 0)	/* signal could turn it off */
+	return -1;
+
       /* We might want to handle EOF here (c == 0) */
       r = _rl_isearch_dispatch (cxt, cxt->lastc);
       if (r <= 0)
-- 
2.50.1

