* [PATCH] Fix reinterpret_cast for classes with multiple inheritance
[not found] <20240320160115.94-1-ssbssa.ref@yahoo.de>
@ 2024-03-20 16:01 ` Hannes Domani
2024-03-20 16:57 ` Tom Tromey
0 siblings, 1 reply; 3+ messages in thread
From: Hannes Domani @ 2024-03-20 16:01 UTC (permalink / raw)
To: gdb-patches
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
---
gdb/testsuite/gdb.cp/casts.cc | 8 ++++++++
gdb/testsuite/gdb.cp/casts.exp | 10 ++++++++++
gdb/valops.c | 11 +++++++++--
3 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/gdb/testsuite/gdb.cp/casts.cc b/gdb/testsuite/gdb.cp/casts.cc
index 5c7f9dc8a1c..eacd8bc0a44 100644
--- a/gdb/testsuite/gdb.cp/casts.cc
+++ b/gdb/testsuite/gdb.cp/casts.cc
@@ -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;
diff --git a/gdb/testsuite/gdb.cp/casts.exp b/gdb/testsuite/gdb.cp/casts.exp
index 7bfc93b1a02..ca82ab084b9 100644
--- a/gdb/testsuite/gdb.cp/casts.exp
+++ b/gdb/testsuite/gdb.cp/casts.exp
@@ -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"
diff --git a/gdb/valops.c b/gdb/valops.c
index be907440a59..0a4e57672ce 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -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); /* pai: chk_val */
+ }
else
error (_("Invalid reinterpret_cast"));
--
2.35.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Fix reinterpret_cast for classes with multiple inheritance
2024-03-20 16:01 ` [PATCH] Fix reinterpret_cast for classes with multiple inheritance Hannes Domani
@ 2024-03-20 16:57 ` Tom Tromey
2024-03-20 17:03 ` Hannes Domani
0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2024-03-20 16:57 UTC (permalink / raw)
To: Hannes Domani; +Cc: gdb-patches
>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
Hannes> Currently a reinterpret_cast may change the pointer value if
Hannes> multiple inheritance is involved:
Thanks for the patch.
Hannes> + else if (dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_PTR)
Hannes> + {
Hannes> + /* Don't do any up- or downcasting. */
Hannes> + result = arg->copy ();
Hannes> + result->deprecated_set_type (dest_type);
Hannes> + result->set_enclosing_type (dest_type);
Hannes> + result->set_pointed_to_offset (0); /* pai: chk_val */
I don't think anybody knows what this comment means in the other spots
it appears. I guess "pai" is some gdb maintainer from the distant past,
though I don't know their real name.
Anyway, please remove this comment.
Ok with this change.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Fix reinterpret_cast for classes with multiple inheritance
2024-03-20 16:57 ` Tom Tromey
@ 2024-03-20 17:03 ` Hannes Domani
0 siblings, 0 replies; 3+ messages in thread
From: Hannes Domani @ 2024-03-20 17:03 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
Am Mittwoch, 20. März 2024 um 17:57:50 MEZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:
> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
>
> Hannes> Currently a reinterpret_cast may change the pointer value if
> Hannes> multiple inheritance is involved:
>
> Thanks for the patch.
>
> Hannes> + else if (dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_PTR)
> Hannes> + {
> Hannes> + /* Don't do any up- or downcasting. */
> Hannes> + result = arg->copy ();
> Hannes> + result->deprecated_set_type (dest_type);
> Hannes> + result->set_enclosing_type (dest_type);
> Hannes> + result->set_pointed_to_offset (0); /* pai: chk_val */
>
> I don't think anybody knows what this comment means in the other spots
> it appears. I guess "pai" is some gdb maintainer from the distant past,
> though I don't know their real name.
>
> Anyway, please remove this comment.
>
> Ok with this change.
> Approved-By: Tom Tromey <tom@tromey.com>
Pushed with this change, thanks.
Hannes
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-03-20 17:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20240320160115.94-1-ssbssa.ref@yahoo.de>
2024-03-20 16:01 ` [PATCH] Fix reinterpret_cast for classes with multiple inheritance Hannes Domani
2024-03-20 16:57 ` Tom Tromey
2024-03-20 17:03 ` Hannes Domani
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).