From d2d1ea20aef6ed97232280b5e15a52b8d7dc7b7d Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 16 Jun 2020 17:48:38 -0600 Subject: [PATCH] Fix crash when TUI window creation fails If a TUI window is written in Python, and if the window construction function fails, then gdb will crash. This patch fixes the crash. gdb/ChangeLog 2020-06-16 Tom Tromey * python/py-tui.c (tui_py_window::~tui_py_window): Handle case where m_window==nullptr. gdb/testsuite/ChangeLog 2020-06-16 Tom Tromey * gdb.python/tui-window.py (failwin): New function. Register it as a TUI window type. * gdb.python/tui-window.exp: Create new "fail" layout. Test it. --- gdb/ChangeLog | 5 +++++ gdb/python/py-tui.c | 5 ++++- gdb/testsuite/ChangeLog | 6 ++++++ gdb/testsuite/gdb.python/tui-window.exp | 3 +++ gdb/testsuite/gdb.python/tui-window.py | 6 ++++++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 08362f291de..cb3761c1ba7 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2020-06-16 Tom Tromey + + * python/py-tui.c (tui_py_window::~tui_py_window): Handle case + where m_window==nullptr. + 2020-06-15 Tom Tromey * windows-nat.c (windows_nat::handle_output_debug_string): diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c index ca88f85eb9f..95c71f1d2dd 100644 --- a/gdb/python/py-tui.c +++ b/gdb/python/py-tui.c @@ -133,7 +133,10 @@ tui_py_window::~tui_py_window () { gdbpy_enter enter_py (get_current_arch (), current_language); - if (PyObject_HasAttrString (m_window.get (), "close")) + /* This can be null if the user-provided Python construction + function failed. */ + if (m_window != nullptr + && PyObject_HasAttrString (m_window.get (), "close")) { gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "close", nullptr)); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index d2ed9db4ebb..97fecf6791b 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2020-06-16 Tom Tromey + + * gdb.python/tui-window.py (failwin): New function. Register it + as a TUI window type. + * gdb.python/tui-window.exp: Create new "fail" layout. Test it. + 2020-06-16 Gary Benson * gdb.python/py-nested-maps.c (create_map): Add missing return diff --git a/gdb/testsuite/gdb.python/tui-window.exp b/gdb/testsuite/gdb.python/tui-window.exp index 503823a2296..f0fdd96e1ad 100644 --- a/gdb/testsuite/gdb.python/tui-window.exp +++ b/gdb/testsuite/gdb.python/tui-window.exp @@ -36,6 +36,7 @@ gdb_test_no_output "source ${remote_python_file}" \ "source ${testfile}.py" gdb_test_no_output "tui new-layout test test 1 status 0 cmd 1" +gdb_test_no_output "tui new-layout fail fail 1 status 0 cmd 1" if {![Term::enter_tui]} { unsupported "TUI not supported" @@ -49,3 +50,5 @@ Term::check_contents "Window display" "Test: 0" Term::resize 51 51 # Remember that a resize request actually does two resizes... Term::check_contents "Window was updated" "Test: 2" + +Term::command "layout fail" diff --git a/gdb/testsuite/gdb.python/tui-window.py b/gdb/testsuite/gdb.python/tui-window.py index 4deb585f138..f362b8768ae 100644 --- a/gdb/testsuite/gdb.python/tui-window.py +++ b/gdb/testsuite/gdb.python/tui-window.py @@ -35,3 +35,9 @@ class TestWindow: self.count = self.count + 1 gdb.register_window_type("test", TestWindow) + +# A TUI window "constructor" that always fails. +def failwin(win): + raise RuntimeError("Whoops") + +gdb.register_window_type("fail", failwin) -- 2.30.2