]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix casting in-memory values of primitive types to const reference
authorHannes Domani <ssbssa@yahoo.de>
Wed, 20 Mar 2024 17:23:40 +0000 (18:23 +0100)
committerHannes Domani <ssbssa@yahoo.de>
Wed, 20 Mar 2024 17:23:50 +0000 (18:23 +0100)
It's currently not possible to cast an in-memory value of a primitive
type to const reference:
```
(gdb) p Q.id
$1 = 42
(gdb) p (int&)Q.id
$2 = (int &) @0x22fd0c: 42
(gdb) p (const int&)Q.id
Attempt to take address of value not located in memory.
```

And if in a function call an argument needs the same kind of casting,
it also doesn't work:
```
(gdb) l f3
39      int f3(const int &i)
40      {
41        return i;
42      }
(gdb) p f3(Q.id)
Attempt to take address of value not located in memory.
```

It's because when the constness of the type changes in a call to
value_cast, a new not_lval value is allocated, which doesn't exist
in the target memory.

Fixed by ignoring const/volatile/restrict qualifications in
value_cast when comparing cast type to original type, so the new
value will point to the same location as the original value:
```
(gdb) p (int&)i
$2 = (int &) @0x39f72c: 1
(gdb) p (const int&)i
$3 = (const int &) @0x39f72c: 1
(gdb) p f3(Q.id)
$4 = 42
```

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

index ca82ab084b9cb7f3b7571241dc0759dcaeaf9ae2..9f7638c8aee26cbb4edf921c214126afe594639d 100644 (file)
@@ -180,6 +180,9 @@ 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 (const int &) gd.left" \
+    " = \\(const int \\&\\) @$nonzero_hex: 23"
+
 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"
index f038d71fe10a29fef0292b6e3b89166824ee47f1..3ef28688607f4a93a73591f63c91ae7547340f2d 100644 (file)
@@ -36,6 +36,11 @@ int f2(Child& C)
   return f1(C);                        /* Set breakpoint marker2 here.  */
 }
 
+int f3(const int &i)
+{
+  return i;
+}
+
 struct OtherParent {
   OtherParent (int other_id0) : other_id(other_id0) { }
   int other_id;
@@ -64,6 +69,7 @@ int main(void)
 
   f2(Q);
   f2(QR);
+  f3(Q.id);
 
   MultiChild MQ(53);
   MultiChild& MQR = MQ;
index 03bb8e62496765e585b7c64781bc3748c272b4a6..e5c28e6e2ad38ecd0be78fb8cf1bfbc47a79cc01 100644 (file)
@@ -62,3 +62,4 @@ gdb_test "print mf2(MQ)" ".* = 106"
 gdb_test "print f1(MQR)" ".* = 53"
 gdb_test "print mf1(MQR)" ".* = 106"
 gdb_test "print mf2(MQR)" ".* = 106"
+gdb_test "print f3(Q.id)" ".* = 42"
index 1a943f0fc78fcf1f4ffb1769613f3475d80d6b52..52cce28728134d0d84c520405897d2362b27ca4e 100644 (file)
@@ -411,7 +411,8 @@ value_cast (struct type *type, struct value *arg2)
      In this case we want to preserve the LVAL of ARG2 as this allows the
      resulting value to be used in more places.  We do this by calling
      VALUE_COPY if appropriate.  */
-  if (types_deeply_equal (arg2->type (), type))
+  if (types_deeply_equal (make_unqualified_type (arg2->type ()),
+                         make_unqualified_type (type)))
     {
       /* If the types are exactly equal then we can avoid creating a new
         value completely.  */