From b90e5339140ee1ff0ff454098c882bf7dcf9bbf9 Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Fri, 10 Apr 2026 15:50:22 +0200 Subject: [PATCH] [gdb] Add selftest for tui_setup_io Add a selftest for the problem described in commit b171f68e945 ("[gdb/tui] Make tui_setup_io more robust"). Reviewed-By: Guinevere Larsen --- gdb/tui/tui-io.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c index 4b4bc818430..642b88ead0c 100644 --- a/gdb/tui/tui-io.c +++ b/gdb/tui/tui-io.c @@ -46,6 +46,7 @@ #include "pager.h" #include "gdbsupport/gdb-checked-static-cast.h" #include "logging-file.h" +#include "gdbsupport/selftest.h" /* This redefines CTRL if it is not already defined, so it must come after terminal state related include files like and @@ -1328,3 +1329,36 @@ tui_getc (FILE *fp) return 0; } } + +#if GDB_SELF_TEST +namespace selftests { +namespace tui { +namespace io { + +static void +run_tests () +{ + if (!tui_active) + { + /* Calling tui_setup_io (0) when tui is disabled should have no effect. */ + tui_setup_io (0); + + /* If the output streams are reduced to nullptrs, then the self-test + infrastructure will crash when trying to report these failures. */ + SELF_CHECK (*redirectable_stdout () != nullptr); + SELF_CHECK (*redirectable_stderr () != nullptr); + SELF_CHECK (*redirectable_stdlog () != nullptr); + } +} + +} /* namespace io */ +} /* namespace tui */ +} /* namespace selftests */ +#endif /* GDB_SELF_TEST */ + +INIT_GDB_FILE (tui_io) +{ +#if GDB_SELF_TEST + selftests::register_test ("tui-io", selftests::tui::io::run_tests); +#endif +} -- 2.47.3