libdw: Don't crash on invalid die in dwarf_dieoffset.
authorMark Wielaard <mark@klomp.org>
Thu, 31 May 2018 11:01:39 +0000 (13:01 +0200)
committerMark Wielaard <mark@klomp.org>
Thu, 31 May 2018 17:48:00 +0000 (19:48 +0200)
Add explicit test in get-units-invalid for dwarf_cuoffset and
dwarf_dieoffset. Make sure dwarf_dieoffset returns (Dwarf_Off) -1
on failure.

Signed-off-by: Mark Wielaard <mark@klomp.org>
libdw/ChangeLog
libdw/dwarf_dieoffset.c
src/ChangeLog
src/readelf.c
tests/ChangeLog
tests/get-units-invalid.c

index 5a33d9c..38b45ba 100644 (file)
@@ -1,3 +1,8 @@
+2018-05-31  Mark Wielaard  <mark@klomp.org>
+
+       * dwarf_dieoffset.c: Check die->cu != NULL. Return -1, not ~0ul
+       on failure.
+
 2018-05-29  Mark Wielaard  <mark@klomp.org>
 
        * dwarf_cuoffset.c (dwarf_cuoffset): Check die->cu is not NULL.
index 8028f6d..3a8e2cb 100644 (file)
@@ -38,8 +38,8 @@
 Dwarf_Off
 dwarf_dieoffset (Dwarf_Die *die)
 {
-  return (die == NULL
-         ? ~0ul
+  return ((die == NULL || die->cu == NULL)
+         ? (Dwarf_Off) -1
          : (Dwarf_Off) (die->addr - die->cu->startp + die->cu->start));
 }
 INTDEF(dwarf_dieoffset)
index f424fb7..03ed5aa 100644 (file)
@@ -1,3 +1,7 @@
+2018-05-31  Mark Wielaard  <mark@klomp.org>
+
+       * readelf.c (print_debug_units): Check offset against -1 not ~0ul.
+
 2018-05-29  Mark Wielaard  <mark@klomp.org>
 
        * readelf.c (print_debug_loc_section): Handle GNU DebugFission list
index 2ccbea5..470a94e 100644 (file)
@@ -7588,7 +7588,7 @@ print_debug_units (Dwfl_Module *dwflmod,
   do
     {
       Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
-      if (unlikely (offset == ~0ul))
+      if (unlikely (offset == (Dwarf_Off) -1))
        {
          if (!silent)
            error (0, 0, gettext ("cannot get DIE offset: %s"),
index b656bee..521df52 100644 (file)
@@ -1,3 +1,8 @@
+2018-05-31  Mark Wielaard  <mark@klomp.org>
+
+       * get-units-invalid.c (main): Check dwarf_cuoffset and
+       dwarf_dieoffset.
+
 2018-05-29  Mark Wielaard  <mark@klomp.org>
 
        * dwarf-die-addr-die.c (check_dbg): Also check subdies, split or
index 58b32c0..ba0f818 100644 (file)
@@ -83,7 +83,19 @@ main (int argc, char *argv[])
              if (dwarf_ranges (&subdie, 0, &base, &start, &end) != -1)
                {
                  printf ("Should NOT have a ranges: %s\n",
-                         dwarf_diename (&result));
+                         dwarf_diename (&subdie));
+                 return -1;
+               }
+             if (dwarf_cuoffset (&subdie) != (Dwarf_Off) -1)
+               {
+                 printf ("Should NOT have a cuoffset: %s\n",
+                         dwarf_diename (&subdie));
+                 return -1;
+               }
+             if (dwarf_dieoffset (&subdie) != (Dwarf_Off) -1)
+               {
+                 printf ("Should NOT have a die offset: %s\n",
+                         dwarf_diename (&subdie));
                  return -1;
                }
            }