From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id C7D47383941D; Sat, 4 Jun 2022 11:17:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7D47383941D Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/fortran] Fix literal truncation X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 1d8c0dfae79a5183e9e3311fb867afd679bc8e84 X-Git-Newrev: a2c0d041fdbf9d661099e31629c96cdd666e8b83 Message-Id: <20220604111748.C7D47383941D@sourceware.org> Date: Sat, 4 Jun 2022 11:17:48 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Jun 2022 11:17:48 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Da2c0d041fdbf= 9d661099e31629c96cdd666e8b83 commit a2c0d041fdbf9d661099e31629c96cdd666e8b83 Author: Tom de Vries Date: Sat Jun 4 13:17:33 2022 +0200 [gdb/fortran] Fix literal truncation =20 As mentioned in commit 5b758627a18 ("Make gdb.base/parse_number.exp tes= t all architectures"): ... There might be a bug that 32-bit fortran truncates 64-bit values to 32-bit, given "p/x 0xffffffffffffffff" returns "0xffffffff". ... =20 More concretely, we have: ... $ for arch in i386:x86-64 i386; do \ gdb -q -batch -ex "set arch $arch" -ex "set lang fortran" \ -ex "p /x 0xffffffffffffffff"; \ done The target architecture is set to "i386:x86-64". $1 =3D 0xffffffffffffffff The target architecture is set to "i386". $1 =3D 0xffffffff ... =20 Fix this by adding a range check in parse_number in gdb/f-exp.y. =20 Furthermore, make sure we error out on overflow instead of truncating i= n all other cases. =20 Tested on x86_64-linux. Diff: --- gdb/f-exp.y | 31 +++++++++++++++--------------= -- gdb/testsuite/gdb.base/parse_number.exp | 4 +--- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/gdb/f-exp.y b/gdb/f-exp.y index 90cc2c65c7b..62641083850 100644 --- a/gdb/f-exp.y +++ b/gdb/f-exp.y @@ -1076,16 +1076,11 @@ parse_number (struct parser_state *par_state, n *=3D base; n +=3D i; } - /* Portably test for overflow (only works for nonzero values, so make - a second check for zero). */ - if ((prevn >=3D n) && n !=3D 0) - unsigned_p=3D1; /* Try something unsigned */ - /* If range checking enabled, portably test for unsigned overflow. = */ - if (RANGE_CHECK && n !=3D 0) - { - if ((unsigned_p && prevn >=3D n)) - range_error (_("Overflow on numeric constant.")); - } + /* Test for overflow. */ + if (prevn =3D=3D 0 && n =3D=3D 0) + ; + else if (RANGE_CHECK && prevn >=3D n) + range_error (_("Overflow on numeric constant.")); prevn =3D n; } =20 @@ -1100,7 +1095,8 @@ parse_number (struct parser_state *par_state, but too many compilers warn about that, when ints and longs are the same size. So we shift it twice, with fewer bits each time, for the same result. */ - =20 + + int bits_available; if ((gdbarch_int_bit (par_state->gdbarch ()) !=3D gdbarch_long_bit (par_state->gdbarch ()) && ((n >> 2) @@ -1108,19 +1104,22 @@ parse_number (struct parser_state *par_state, shift warning */ || long_p) { - high_bit =3D ((ULONGEST)1) - << (gdbarch_long_bit (par_state->gdbarch ())-1); + bits_available =3D gdbarch_long_bit (par_state->gdbarch ()); unsigned_type =3D parse_type (par_state)->builtin_unsigned_long; signed_type =3D parse_type (par_state)->builtin_long; - } + } else=20 { - high_bit =3D - ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1); + bits_available =3D gdbarch_int_bit (par_state->gdbarch ()); unsigned_type =3D parse_type (par_state)->builtin_unsigned_int; signed_type =3D parse_type (par_state)->builtin_int; } =20 + high_bit =3D ((ULONGEST)1) << (bits_available - 1); =20 + if (RANGE_CHECK + && ((n >> 2) >> (bits_available - 2))) + range_error (_("Overflow on numeric constant.")); + putithere->typed_val.val =3D n; =20 /* If the high bit of the worked out type is set then this number diff --git a/gdb/testsuite/gdb.base/parse_number.exp b/gdb/testsuite/gdb.ba= se/parse_number.exp index 638ea342384..87554ccf995 100644 --- a/gdb/testsuite/gdb.base/parse_number.exp +++ b/gdb/testsuite/gdb.base/parse_number.exp @@ -176,9 +176,7 @@ proc parse_number { lang n } { return [list "unsigned long" $n] } else { # Overflow. - # Some truncated value or re_overflow, should be re_overflow. - return [list "((unsigned )?(int|long)|$re_overflow)" \ - ($any|$re_overflow)] + return [list $re_overflow $re_overflow] } } else { if { [c_like $lang] } {