public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libdw: Use signedness of subrange type to determine array bounds
@ 2021-10-06 20:41 Mark Wielaard
  0 siblings, 0 replies; only message in thread
From: Mark Wielaard @ 2021-10-06 20:41 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

When calculating the array size check if the subrange has an associate
type, if it does then check the type to determine whether the upper
and lower values need to be interpreted as signed of unsigned
values. We default to signed because that is what the testcase
run-aggregate-size.sh testfile-size4 expects (this is an hardwritten
testcase, we could have chosen a different default).

https://sourceware.org/bugzilla/show_bug.cgi?id=28294

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdw/ChangeLog              |  6 +++++
 libdw/dwarf_aggregate_size.c | 44 +++++++++++++++++++++++++++++++-----
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index b707bbfe..4275b830 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,9 @@
+2021-10-06  Mark Wielaard  <mark@klomp.org>
+
+	* dwarf_aggregate_size.c (array_size): Check signedness of child DIE
+	type. Use dwarf_formsdata or dwarf_formudata to get the lower and
+	upper bounds.
+
 2021-09-08  Mark Wielaard  <mark@klomp.org>
 
 	* dwarf_begin_elf.c (valid_p): Identify ELF class and use this to set
diff --git a/libdw/dwarf_aggregate_size.c b/libdw/dwarf_aggregate_size.c
index 75105e4d..96023d69 100644
--- a/libdw/dwarf_aggregate_size.c
+++ b/libdw/dwarf_aggregate_size.c
@@ -83,19 +83,51 @@ array_size (Dwarf_Die *die, Dwarf_Word *size,
 	    }
 	  else
 	    {
+	      bool is_signed = true;
+	      if (INTUSE(dwarf_attr) (get_type (&child, attr_mem, &type_mem),
+				      DW_AT_encoding, attr_mem) != NULL)
+		{
+		  Dwarf_Word encoding;
+		  if (INTUSE(dwarf_formudata) (attr_mem, &encoding) == 0)
+		    is_signed = (encoding == DW_ATE_signed
+				 || encoding == DW_ATE_signed_char);
+		}
+
 	      Dwarf_Sword upper;
 	      Dwarf_Sword lower;
-	      if (INTUSE(dwarf_formsdata) (INTUSE(dwarf_attr_integrate)
-					   (&child, DW_AT_upper_bound,
-					    attr_mem), &upper) != 0)
-		return -1;
+	      if (is_signed)
+		{
+		  if (INTUSE(dwarf_formsdata) (INTUSE(dwarf_attr_integrate)
+					       (&child, DW_AT_upper_bound,
+						attr_mem), &upper) != 0)
+		    return -1;
+		}
+	      else
+		{
+		  Dwarf_Word unsigned_upper;
+		  if (INTUSE(dwarf_formudata) (INTUSE(dwarf_attr_integrate)
+					       (&child, DW_AT_upper_bound,
+						attr_mem), &unsigned_upper) != 0)
+		    return -1;
+		  upper = unsigned_upper;
+		}
 
 	      /* Having DW_AT_lower_bound is optional.  */
 	      if (INTUSE(dwarf_attr_integrate) (&child, DW_AT_lower_bound,
 						attr_mem) != NULL)
 		{
-		  if (INTUSE(dwarf_formsdata) (attr_mem, &lower) != 0)
-		    return -1;
+		  if (is_signed)
+		    {
+		      if (INTUSE(dwarf_formsdata) (attr_mem, &lower) != 0)
+			return -1;
+		    }
+		  else
+		    {
+		      Dwarf_Word unsigned_lower;
+		      if (INTUSE(dwarf_formudata) (attr_mem, &unsigned_lower) != 0)
+			return -1;
+		      lower = unsigned_lower;
+		    }
 		}
 	      else
 		{
-- 
2.32.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-06 20:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-06 20:41 [PATCH] libdw: Use signedness of subrange type to determine array bounds Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).