Avoid crash in write_psymtabs_to_index
authorTom Tromey <tom@tromey.com>
Sat, 17 Apr 2021 19:40:04 +0000 (13:40 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 17 Apr 2021 19:40:04 +0000 (13:40 -0600)
If I try "save gdb-index" using the executable from
gdb.cp/cmpd-minsyms.exp, gdb will crash.  This happens due to a
missing NULL check.

gdb/ChangeLog
2021-04-17  Tom Tromey  <tom@tromey.com>

* dwarf2/index-write.c (write_psymtabs_to_index): Check
partial_symtabs.

gdb/testsuite/ChangeLog
2021-04-17  Tom Tromey  <tom@tromey.com>

* gdb.dwarf2/gdb-index-nodebug.exp: New file.

gdb/ChangeLog
gdb/dwarf2/index-write.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.dwarf2/gdb-index-nodebug.exp [new file with mode: 0644]

index 33a0997afcc85be3216b48d4bd9421684b0ec05b..803597532a7184f8d559cd23711932f8dde93b09 100644 (file)
@@ -1,3 +1,8 @@
+2021-04-17  Tom Tromey  <tom@tromey.com>
+
+       * dwarf2/index-write.c (write_psymtabs_to_index): Check
+       partial_symtabs.
+
 2021-04-17  Tom Tromey  <tom@tromey.com>
 
        * psymtab.c (psymbol_functions::expand_matching_symbols): Rename
index f42a25331f8649f5224fcf879060286d4e22883d..6cfe415c5a2d18917631e50d4a658dc8cf3bd52f 100644 (file)
@@ -1693,7 +1693,8 @@ write_psymtabs_to_index (dwarf2_per_objfile *per_objfile, const char *dir,
   if (per_objfile->per_bfd->types.size () > 1)
     error (_("Cannot make an index when the file has multiple .debug_types sections"));
 
-  if (!per_bfd->partial_symtabs->psymtabs
+  if (per_bfd->partial_symtabs == nullptr
+      || !per_bfd->partial_symtabs->psymtabs
       || !per_bfd->partial_symtabs->psymtabs_addrmap)
     return;
 
index 995bb783bee815031a9d727fae507c8873ee482c..3e4a384bd95abcc956470f9ae04cc0d668579a94 100644 (file)
@@ -1,3 +1,7 @@
+2021-04-17  Tom Tromey  <tom@tromey.com>
+
+       * gdb.dwarf2/gdb-index-nodebug.exp: New file.
+
 2021-04-16  Tom Tromey  <tom@tromey.com>
 
        * gdb.rust/simple.exp: Change error text.
diff --git a/gdb/testsuite/gdb.dwarf2/gdb-index-nodebug.exp b/gdb/testsuite/gdb.dwarf2/gdb-index-nodebug.exp
new file mode 100644 (file)
index 0000000..27b46d4
--- /dev/null
@@ -0,0 +1,28 @@
+# Copyright 2021 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/>.
+
+load_lib dwarf.exp
+
+standard_testfile main.c
+
+if {[prepare_for_testing "failed to prepare" "${testfile}" \
+        [list ${srcfile}] {nodebug}]} {
+    return -1
+}
+
+set index_file ${testfile}.gdb-index
+# The bug was that gdb would crash here.
+gdb_test_no_output "save gdb-index [file dirname ${index_file}]" \
+    "try to save gdb index"