public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] libdw: Use signedness of subrange type to determine array bounds
Date: Wed,  6 Oct 2021 22:41:29 +0200	[thread overview]
Message-ID: <20211006204129.29845-1-mark@klomp.org> (raw)

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


                 reply	other threads:[~2021-10-06 20:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211006204129.29845-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).