[PATCH] libdw: Don't crash on invalid die in dwarf_dieoffset.

Mark Wielaard mark@klomp.org
Thu May 31 11:02:00 GMT 2018


Add explicit test in get-units-invalid for dwarf_cuoffset and
dwarf_dieoffset.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdw/ChangeLog           |  4 ++++
 libdw/dwarf_dieoffset.c   |  2 +-
 tests/ChangeLog           |  5 +++++
 tests/get-units-invalid.c | 14 +++++++++++++-
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index c304a3b..61bf14f 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,7 @@
+2018-05-31  Mark Wielaard  <mark@klomp.org>
+
+	* dwarf_dieoffset.c: Check die->cu != NULL.
+
 2018-05-30  Mark Wielaard  <mark@klomp.org>
 
 	* libdw/dwarf_getsrclines.c (read_srclines): Change ndir and
diff --git a/libdw/dwarf_dieoffset.c b/libdw/dwarf_dieoffset.c
index 8028f6d..c869ecd 100644
--- a/libdw/dwarf_dieoffset.c
+++ b/libdw/dwarf_dieoffset.c
@@ -38,7 +38,7 @@
 Dwarf_Off
 dwarf_dieoffset (Dwarf_Die *die)
 {
-  return (die == NULL
+  return ((die == NULL || die->cu == NULL)
 	  ? ~0ul
 	  : (Dwarf_Off) (die->addr - die->cu->startp + die->cu->start));
 }
diff --git a/tests/ChangeLog b/tests/ChangeLog
index b656bee..521df52 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -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
diff --git a/tests/get-units-invalid.c b/tests/get-units-invalid.c
index 58b32c0..ba0f818 100644
--- a/tests/get-units-invalid.c
+++ b/tests/get-units-invalid.c
@@ -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;
 		}
 	    }
-- 
1.8.3.1



More information about the Elfutils-devel mailing list