Fix printing of global variable stubs if no inferior is running

Message ID 20231208174128.2632-1-ssbssa@yahoo.de
State New
Headers
Series Fix printing of global variable stubs if no inferior is running |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_gdb_build--master-arm warning Patch is already merged

Commit Message

Hannes Domani Dec. 8, 2023, 5:41 p.m. UTC
  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>
```
---
 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(-)
 create mode 100644 gdb/testsuite/gdb.cp/print-global-stub.cc
 create mode 100644 gdb/testsuite/gdb.cp/print-global-stub.exp
  

Comments

Tom Tromey Dec. 8, 2023, 5:49 p.m. UTC | #1
>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> Since 3c45e9f915ae4aeab7312d6fc55a947859057572 gdb crashes when trying
Hannes> to print a global variable stub without a running inferior, because of
Hannes> a missing nullptr-check (the block_scope function took care of that
Hannes> check before it was converted to a method).

Ugh, sorry about that.

This is ok.  Also ok for the 14 branch.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Hannes Domani Dec. 8, 2023, 6:15 p.m. UTC | #2
Am Freitag, 8. Dezember 2023, 18:49:28 MEZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:

> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
>
> Hannes> Since 3c45e9f915ae4aeab7312d6fc55a947859057572 gdb crashes when trying
> Hannes> to print a global variable stub without a running inferior, because of
> Hannes> a missing nullptr-check (the block_scope function took care of that
> Hannes> check before it was converted to a method).
>
> Ugh, sorry about that.
>
> This is ok.  Also ok for the 14 branch.
> Approved-By: Tom Tromey <tom@tromey.com>

Thanks, pushed to both master and gdb-14-branch, plus the following new
ticket, since it's required for the 14 branch:
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31128
  

Patch

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>"