public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Fix printing of global variable stubs if no inferior is running
@ 2023-12-08 18:08 Hannes Domani
  0 siblings, 0 replies; only message in thread
From: Hannes Domani @ 2023-12-08 18:08 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=576745e26c0ec76a53ba45b20af464628a50b3e4

commit 576745e26c0ec76a53ba45b20af464628a50b3e4
Author: Hannes Domani <ssbssa@yahoo.de>
Date:   Fri Dec 8 19:06:14 2023 +0100

    Fix printing of global variable stubs if no inferior is running
    
    Since 3c45e9f915ae4aeab7312d6fc55a947859057572 gdb crashes when trying
    to print a global variable stub without a running inferior, because of
    a missing nullptr-check (the block_scope function took care of that
    check before it was converted to a method).
    
    With this check it works again:
    ```
    (gdb) print s
    $1 = <incomplete type>
    ```
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31128
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/cp-namespace.c                         |  6 +++++-
 gdb/testsuite/gdb.cp/print-global-stub.cc  | 31 +++++++++++++++++++++++++++++
 gdb/testsuite/gdb.cp/print-global-stub.exp | 32 ++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index 975e789f18a..1ec00dc7fde 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -1027,7 +1027,11 @@ cp_lookup_transparent_type (const char *name)
 
   /* If that doesn't work and we're within a namespace, look there
      instead.  */
-  scope = get_selected_block (0)->scope ();
+  const block *block = get_selected_block (0);
+  if (block == nullptr)
+    return nullptr;
+
+  scope = block->scope ();
 
   if (scope[0] == '\0')
     return NULL;
diff --git a/gdb/testsuite/gdb.cp/print-global-stub.cc b/gdb/testsuite/gdb.cp/print-global-stub.cc
new file mode 100644
index 00000000000..bc12707e87e
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/print-global-stub.cc
@@ -0,0 +1,31 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2023 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+struct S
+{
+  S (int);
+  virtual ~S ();
+
+  int m_i;
+};
+
+S s (5);
+
+int main ()
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.cp/print-global-stub.exp b/gdb/testsuite/gdb.cp/print-global-stub.exp
new file mode 100644
index 00000000000..039e8251afe
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/print-global-stub.exp
@@ -0,0 +1,32 @@
+# Copyright (C) 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite.
+# It tests printing of a global stub without inferior.
+
+require allow_cplus_tests
+
+standard_testfile .cc
+set objfile [standard_output_file ${testfile}.o]
+
+if { [gdb_compile $srcdir/$subdir/$srcfile $objfile object \
+	  {c++ debug}] != "" } {
+    untested "failed to compile"
+    return -1
+}
+
+clean_restart $objfile
+
+gdb_test "print s" " = <incomplete type>"

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-12-08 18:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-08 18:08 [binutils-gdb] Fix printing of global variable stubs if no inferior is running Hannes Domani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).