From a776957c8c3a9177345ee7ca91077234ed7f508e Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 22 Apr 2019 11:46:47 -0600 Subject: Fix crash in dwarf2read.c with template parameters PR c++/24470 concerns a crash in dwarf2read.c that occurs with a particular test case. The issue turns out to be that process_structure_scope will pass NULL to symbol_symtab. This happens because new_symbol decided not to create a symbol for the particular DIE. This patch fixes the problem by finding another reasonably-appropriate symtab to use instead; issuing a complaint if one cannot be found for some reason. As mentioned in the bug, I think there are other bugs here. For example, when using "ptype" on the "l" object in the test case, I think I would expect to see the template parameter. I didn't research this too closely, since it seemed more important to fix the crash. Tested on x86-64 Fedora 29. I'd like to check this in to the 8.3 branch as well. 2019-04-30 Tom Tromey PR c++/24470: * dwarf2read.c (process_structure_scope): Handle case where type has template parameters but no symbol was created. gdb/testsuite/ChangeLog 2019-04-30 Tom Tromey PR c++/24470: * gdb.cp/temargs.cc: Add test code from PR. --- gdb/dwarf2read.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'gdb/dwarf2read.c') diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c index 571fe1e..751c59c3 100644 --- a/gdb/dwarf2read.c +++ b/gdb/dwarf2read.c @@ -16212,13 +16212,34 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu) if (has_template_parameters) { - /* Make sure that the symtab is set on the new symbols. - Even though they don't appear in this symtab directly, - other parts of gdb assume that symbols do, and this is - reasonably true. */ - for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i) - symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), - symbol_symtab (sym)); + struct symtab *symtab; + if (sym != nullptr) + symtab = symbol_symtab (sym); + else if (cu->line_header != nullptr) + { + /* Any related symtab will do. */ + symtab + = cu->line_header->file_name_at (file_name_index (1))->symtab; + } + else + { + symtab = nullptr; + complaint (_("could not find suitable " + "symtab for template parameter" + " - DIE at %s [in module %s]"), + sect_offset_str (die->sect_off), + objfile_name (objfile)); + } + + if (symtab != nullptr) + { + /* Make sure that the symtab is set on the new symbols. + Even though they don't appear in this symtab directly, + other parts of gdb assume that symbols do, and this is + reasonably true. */ + for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i) + symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab); + } } } } -- cgit v1.1