[binutils-gdb] [gdb/tui] Make tui_setup_io more robust

Tom de Vries via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b171f68e9450e3bba074b0fe918b78c2ebb03af8

commit b171f68e9450e3bba074b0fe918b78c2ebb03af8
Author: Tom de Vries <[email protected]>
Date:   Thu Apr 2 23:09:09 2026 +0200

    [gdb/tui] Make tui_setup_io more robust
    
    The following sequence of events may happen when enabling TUI:
    - tui_enable () is called,
    - a SIGFPE happens, before tui_setup_io (1) is called,
    - the SIGFPE triggers handle_fatal_signal, which calls tui_disable (),
    - during tui_disable (), tui_setup_io (0) is called, and
    - tui_setup_io (0) tries to restore things that were saved during a previous
      tui_setup_io (1) call, but tui_setup_io (1) was never called so the saving
      never happened.
    
    This can cause current_ui.m_ui_stderr to be nullptr, which then can cause a
    crash in sig_write (PR33918).
    
    Fix this by:
    - adding a variable tui_io_mode, and
    - using the variable to bail out of tui_setup_io in case the current mode
      doesn't change.
    
    Tested on x86_64-linux.
    
    Approved-By: Andrew Burgess <[email protected]>
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33918

Diff:
---
 gdb/tui/tui-io.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index e0892a40c0d..4b4bc818430 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -818,6 +818,10 @@ tui_rl_display_match_list (char **matches, int len, int max)
   gdb_display_match_list (matches, len, max, &displayer);
 }
 
+/* Whether the IO is setup for curses (1) or non-curses (0).  */
+
+static int tui_io_mode;
+
 /* Setup the IO for curses or non-curses mode.
    - In non-curses mode, readline and gdb use the standard input and
    standard output/error directly.
@@ -831,6 +835,14 @@ tui_setup_io (int mode)
 {
   extern int _rl_echoing_p;
 
+  if (tui_io_mode == mode)
+    {
+      /* Nothing to do.  Also, this makes sure that we don't try to restore
+	 things before having saved them. */
+      return;
+    }
+  tui_io_mode = mode;
+
   if (mode)
     {
       /* Ensure that readline has been initialized before saving any
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.