[PATCH] Fix C++ cast of derived class to base class

Tom de Vries tdevries@suse.de
Wed May 4 17:27:56 GMT 2022


On 4/18/22 17:34, Tom Tromey wrote:
>>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:
> 
> Tom> PR c++/28907 points out that casting from a derived class to a base
> Tom> class fails in some situations.  The problem turned out to be a
> Tom> missing use of value_embedded_offset.  One peculiarity here is that,
> Tom> if you managed to construct a pointer-to-derived with an embedded
> Tom> offset of 0, the cast would work -- for example, one of the two new
> Tom> tests here passes without the patch.
> 
> I'm checking this in now.

The added tests fail with target board unix/-m32.

In more detail, with some additional printing:
...
(gdb) print /x &gd^M
$31 = 0xffffc5c8^M
(gdb) PASS: gdb.cp/casts.exp: print /x &gd
print /x (unsigned long long)&gd^M
$32 = 0xffffc5c8^M
(gdb) PASS: gdb.cp/casts.exp: print /x (unsigned long long)&gd
print /x gd_value^M
$33 = 0xffffffffffffc5c8^M
(gdb) PASS: gdb.cp/casts.exp: print /x gd_value
print (unsigned long long) &gd == gd_value^M
$34 = false^M
(gdb) FAIL: gdb.cp/casts.exp: print (unsigned long long) &gd == gd_value
...

This fixes it:
...
diff --git a/gdb/testsuite/gdb.cp/casts.cc b/gdb/testsuite/gdb.cp/casts.cc
index ea4dc961793..f64d13c26df 100644
--- a/gdb/testsuite/gdb.cp/casts.cc
+++ b/gdb/testsuite/gdb.cp/casts.cc
@@ -1,3 +1,5 @@
+#include <cstdint>
+
  struct A
  {
    int a;
@@ -65,7 +67,7 @@ main (int argc, char **argv)
    LeftRight gd;
    gd.left = 23;
    gd.right = 27;
-  unsigned long long gd_value = (unsigned long long) &gd;
+  unsigned long long gd_value = (unsigned long long) (std::uintptr_t)&gd;
    unsigned long long r_value = (unsigned long long) (Right *) &gd;

    return 0;  /* breakpoint spot: casts.exp: 1 */
...

WDYT?

Thanks,
- Tom


More information about the Gdb-patches mailing list