]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/dwarf: use offset in dwarf_expr_context::read_mem
authorSimon Marchi <simon.marchi@polymtl.ca>
Tue, 18 Nov 2025 03:38:23 +0000 (22:38 -0500)
committerSimon Marchi <simon.marchi@polymtl.ca>
Wed, 19 Nov 2025 19:41:57 +0000 (14:41 -0500)
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>
gdb/dwarf2/expr.c

index 2125577bec224d0d48114ec166d7b5510e95498a..aa7b8cd95481475a2081a03782cfa179e394431a 100644 (file)
@@ -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;
        }
     }