]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix reinterpret_cast for classes with multiple inheritance
authorHannes Domani <ssbssa@yahoo.de>
Wed, 20 Mar 2024 17:02:06 +0000 (18:02 +0100)
committerHannes Domani <ssbssa@yahoo.de>
Wed, 20 Mar 2024 17:02:06 +0000 (18:02 +0100)
Currently a reinterpret_cast may change the pointer value if
multiple inheritance is involved:
```
(gdb) p r
$1 = (Right *) 0x22f75c
(gdb) p reinterpret_cast<LeftRight*>(r)
$2 = (LeftRight *) 0x22f758
```

It's because value_cast is called in this case, which automatically
does up- and downcasting.

Fixed by simply using the target pointer type in a copy of the
original value:
```
(gdb) p r
$1 = (Right *) 0x3bf87c
(gdb) p reinterpret_cast<LeftRight*>(r)
$2 = (LeftRight *) 0x3bf87c
```

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=18861
Approved-By: Tom Tromey <tom@tromey.com>
gdb/testsuite/gdb.cp/casts.cc
gdb/testsuite/gdb.cp/casts.exp
gdb/valops.c

index 5c7f9dc8a1ce1ba2ffb4e397ea68e51dad6c63ca..eacd8bc0a44e1e9cbb2dcfab9f11d649b5511a44 100644 (file)
@@ -88,6 +88,14 @@ main (int argc, char **argv)
   unsigned long long gd_value = (unsigned long long) (std::uintptr_t)&gd;
   unsigned long long r_value = (unsigned long long) (Right *) &gd;
 
+  LeftRight *lr = &gd;
+  Left *l = lr;
+  Right *r = lr;
+  LeftRight *lr_l = reinterpret_cast<LeftRight *>(l);
+  LeftRight *lr_r = reinterpret_cast<LeftRight *>(r);
+  Left *l_lr = reinterpret_cast<Left *>(lr);
+  Right *r_lr = reinterpret_cast<Right *>(lr);
+
   VirtualLeftRight *vlr = new VirtualLeftRight ();
   VirtualLeft *vl = vlr;
   VirtualRight *vr = vlr;
index 7bfc93b1a0250e820f73bd50e48553542685495f..ca82ab084b9cb7f3b7571241dc0759dcaeaf9ae2 100644 (file)
@@ -180,6 +180,16 @@ gdb_test "print (unsigned long long) (LeftRight *) (Right *) &gd == gd_value" \
 gdb_test "print (unsigned long long) (LeftRight *) (Right *) r_value == gd_value" \
     " = true"
 
+gdb_test "print reinterpret_cast<LeftRight *>(l) == lr_l" " = true"
+gdb_test "print reinterpret_cast<LeftRight *>(r) == lr_r" " = true"
+gdb_test "print reinterpret_cast<Left *>(lr) == l_lr" " = true"
+gdb_test "print reinterpret_cast<Right *>(lr) == r_lr" " = true"
+
+gdb_test "print &reinterpret_cast<LeftRight &>(*l) == lr_l" " = true"
+gdb_test "print &reinterpret_cast<LeftRight &>(*r) == lr_r" " = true"
+gdb_test "print &reinterpret_cast<Left &>(*lr) == l_lr" " = true"
+gdb_test "print &reinterpret_cast<Right &>(*lr) == r_lr" " = true"
+
 gdb_test "print dynamic_cast<VirtualLeftRight *> (vlr) == vlr" " = true"
 gdb_test "print dynamic_cast<VirtualLeftRight *> (vl) == vlr" " = true"
 gdb_test "print dynamic_cast<VirtualLeftRight *> (vr) == vlr" " = true"
index be907440a59ba75f8eac7fed6c97b9997bd47ef0..1a943f0fc78fcf1f4ffb1769613f3475d80d6b52 100644 (file)
@@ -694,10 +694,17 @@ value_reinterpret_cast (struct type *type, struct value *arg)
       || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
       || (dest_code == arg_code
-         && (dest_code == TYPE_CODE_PTR
-             || dest_code == TYPE_CODE_METHODPTR
+         && (dest_code == TYPE_CODE_METHODPTR
              || dest_code == TYPE_CODE_MEMBERPTR)))
     result = value_cast (dest_type, arg);
+  else if (dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_PTR)
+    {
+      /* Don't do any up- or downcasting.  */
+      result = arg->copy ();
+      result->deprecated_set_type (dest_type);
+      result->set_enclosing_type (dest_type);
+      result->set_pointed_to_offset (0);
+    }
   else
     error (_("Invalid reinterpret_cast"));