From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2111) id 6BF663858D28; Wed, 20 Mar 2024 17:25:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6BF663858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1710955523; bh=IDmXZ/muQ37ZnNDqJdIlpuAoWjYW49lLUXSMUni1mAc=; h=From:To:Subject:Date:From; b=skrU8QMQG16xiYh4xLpaAsMd4U9DxVMxCAJpDTbMEAToDawlgsf1UDM9iRk5kcZg8 yugdpHFOVWM6yzZc64A6WQDd22lLcn31scJmyfra0OFlaWNzCtxCTvkFxe2m5qZUTq u3xb9BSRhvjKlZl8EcmN8x57GWq8YzowJIi7wnCE= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Hannes Domani To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Fix casting in-memory values of primitive types to const reference X-Act-Checkin: binutils-gdb X-Git-Author: Hannes Domani X-Git-Refname: refs/heads/master X-Git-Oldrev: 23cdd9431ad424b092c65419d47ef4601168a1c9 X-Git-Newrev: d391f3721e20d160909a3afae7fee647ea5575a2 Message-Id: <20240320172523.6BF663858D28@sourceware.org> Date: Wed, 20 Mar 2024 17:25:23 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dd391f3721e20= d160909a3afae7fee647ea5575a2 commit d391f3721e20d160909a3afae7fee647ea5575a2 Author: Hannes Domani Date: Wed Mar 20 18:23:40 2024 +0100 Fix casting in-memory values of primitive types to const reference =20 It's currently not possible to cast an in-memory value of a primitive type to const reference: ``` (gdb) p Q.id $1 =3D 42 (gdb) p (int&)Q.id $2 =3D (int &) @0x22fd0c: 42 (gdb) p (const int&)Q.id Attempt to take address of value not located in memory. ``` =20 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. ``` =20 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. =20 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 =3D (int &) @0x39f72c: 1 (gdb) p (const int&)i $3 =3D (const int &) @0x39f72c: 1 (gdb) p f3(Q.id) $4 =3D 42 ``` =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D19423 Approved-By: Tom Tromey Diff: --- gdb/testsuite/gdb.cp/casts.exp | 3 +++ gdb/testsuite/gdb.cp/ref-params.cc | 6 ++++++ gdb/testsuite/gdb.cp/ref-params.exp | 1 + gdb/valops.c | 3 ++- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/gdb/testsuite/gdb.cp/casts.exp b/gdb/testsuite/gdb.cp/casts.exp index ca82ab084b9..9f7638c8aee 100644 --- a/gdb/testsuite/gdb.cp/casts.exp +++ b/gdb/testsuite/gdb.cp/casts.exp @@ -180,6 +180,9 @@ gdb_test "print (unsigned long long) (LeftRight *) (Rig= ht *) &gd =3D=3D gd_value" \ gdb_test "print (unsigned long long) (LeftRight *) (Right *) r_value =3D= =3D gd_value" \ " =3D true" =20 +gdb_test "print (const int &) gd.left" \ + " =3D \\(const int \\&\\) @$nonzero_hex: 23" + gdb_test "print reinterpret_cast(l) =3D=3D lr_l" " =3D true" gdb_test "print reinterpret_cast(r) =3D=3D lr_r" " =3D true" gdb_test "print reinterpret_cast(lr) =3D=3D l_lr" " =3D true" diff --git a/gdb/testsuite/gdb.cp/ref-params.cc b/gdb/testsuite/gdb.cp/ref-= params.cc index f038d71fe10..3ef28688607 100644 --- a/gdb/testsuite/gdb.cp/ref-params.cc +++ b/gdb/testsuite/gdb.cp/ref-params.cc @@ -36,6 +36,11 @@ int f2(Child& C) return f1(C); /* Set breakpoint marker2 here. */ } =20 +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) =20 f2(Q); f2(QR); + f3(Q.id); =20 MultiChild MQ(53); MultiChild& MQR =3D MQ; diff --git a/gdb/testsuite/gdb.cp/ref-params.exp b/gdb/testsuite/gdb.cp/ref= -params.exp index 03bb8e62496..e5c28e6e2ad 100644 --- a/gdb/testsuite/gdb.cp/ref-params.exp +++ b/gdb/testsuite/gdb.cp/ref-params.exp @@ -62,3 +62,4 @@ gdb_test "print mf2(MQ)" ".* =3D 106" gdb_test "print f1(MQR)" ".* =3D 53" gdb_test "print mf1(MQR)" ".* =3D 106" gdb_test "print mf2(MQR)" ".* =3D 106" +gdb_test "print f3(Q.id)" ".* =3D 42" diff --git a/gdb/valops.c b/gdb/valops.c index 1a943f0fc78..52cce287281 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -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. */