[v2,17/20] Change die_info methods to check the attribute's form

Message ID 20200404144320.18851-18-tom@tromey.com
State New
Headers show
Series
  • Make DWARF attribute references safe
Related show

Commit Message

Tom Tromey April 4, 2020, 2:43 p.m.
This changes two die_info methods to check the form of the attribute
before using it.

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

	* dwarf2/die.h (struct die_info) <addr_base, ranges_base>: Check
	the attribute's form.
---
 gdb/ChangeLog    |  5 +++++
 gdb/dwarf2/die.h | 22 +++++++++++++++++-----
 2 files changed, 22 insertions(+), 5 deletions(-)

-- 
2.17.2

Patch

diff --git a/gdb/dwarf2/die.h b/gdb/dwarf2/die.h
index 5522ebdf311..4bc15d631e3 100644
--- a/gdb/dwarf2/die.h
+++ b/gdb/dwarf2/die.h
@@ -20,6 +20,8 @@ 
 #ifndef GDB_DWARF2_DIE_H
 #define GDB_DWARF2_DIE_H
 
+#include "complaints.h"
+
 /* This data structure holds a complete die structure.  */
 struct die_info
 {
@@ -40,10 +42,15 @@  struct die_info
   {
     for (unsigned i = 0; i < num_attrs; ++i)
       if (attrs[i].name == DW_AT_addr_base
-	  || attrs[i].name == DW_AT_GNU_addr_base)
+	   || attrs[i].name == DW_AT_GNU_addr_base)
 	{
-	  /* If both exist, just use the first one.  */
-	  return DW_UNSND (&attrs[i]);
+	  if (attrs[i].form_is_unsigned ())
+	    {
+	      /* If both exist, just use the first one.  */
+	      return attrs[i].as_unsigned ();
+	    }
+	  complaint (_("address base attribute (offset %s) as wrong form"),
+		     sect_offset_str (sect_off));
 	}
     return gdb::optional<ULONGEST> ();
   }
@@ -57,8 +64,13 @@  struct die_info
       if (attrs[i].name == DW_AT_rnglists_base
 	  || attrs[i].name == DW_AT_GNU_ranges_base)
 	{
-	  /* If both exist, just use the first one.  */
-	  return DW_UNSND (&attrs[i]);
+	  if (attrs[i].form_is_unsigned ())
+	    {
+	      /* If both exist, just use the first one.  */
+	      return attrs[i].as_unsigned ();
+	    }
+	  complaint (_("ranges base attribute (offset %s) as wrong form"),
+		     sect_offset_str (sect_off));
 	}
     return 0;
   }