[binutils-gdb] gdb/dwarf: use offset in dwarf_expr_context::read_mem

Simon Marchi simark@sourceware.org
Wed Nov 19 19:43:00 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=cc4c8e4ca8b9fe7f04e2ee4614cca96e9cc90044

commit cc4c8e4ca8b9fe7f04e2ee4614cca96e9cc90044
Author: Simon Marchi <simon.marchi@polymtl.ca>
Date:   Mon Nov 17 22:38:23 2025 -0500

    gdb/dwarf: use offset in dwarf_expr_context::read_mem
    
    The `offset` variable is the offset within the passed-in object where
    `addr` falls.  We use it to verify whether `addr` falls within that
    object's bounds, but then the memcpy fails to consider it, meaning that
    we always copy from the beginning of the passed-in object, even if
    `addr` lands in the middle of the object.  Fix that by adding `offset`
    to the source argument of the memcpy.  I caught this by writing a patch
    later in this series, so this fix is covered by the test added in that
    later patch.
    
    Also, I find it a bit odd to compute the offset of `addr` within the
    passed-in object, before knowing if `addr` even lands within the
    passed-in object's address range.  If `addr` is before the object's
    address, it does an unsigned underflow, which I guess works, but is not
    really intuitive.  Change it to check whether `addr` falls within the
    object first, and if so, compute the offset of `addr` within the object.
    
    Change-Id: Ibbacab6d57e693e02e2bdfec4f3a7d42d9a1bd4b
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/dwarf2/expr.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index 2125577bec2..aa7b8cd9548 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -865,12 +865,12 @@ dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr,
   /* Prefer the passed-in memory, if it exists.  */
   if (this->m_addr_info != nullptr)
     {
-      CORE_ADDR offset = addr - this->m_addr_info->addr;
-
-      if (offset < this->m_addr_info->valaddr.size ()
-	  && offset + length <= this->m_addr_info->valaddr.size ())
+      if (addr >= this->m_addr_info->addr
+	  && addr + length <= (this->m_addr_info->addr
+			       + this->m_addr_info->valaddr.size ()))
 	{
-	  memcpy (buf, this->m_addr_info->valaddr.data (), length);
+	  CORE_ADDR offset = addr - this->m_addr_info->addr;
+	  memcpy (buf, this->m_addr_info->valaddr.data () + offset, length);
 	  return;
 	}
     }


More information about the Gdb-cvs mailing list