From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id r/S+F1vahGKZ5QYAWB0awg (envelope-from ) for ; Wed, 18 May 2022 07:36:59 -0400 Received: by simark.ca (Postfix, from userid 112) id 53B0A1E220; Wed, 18 May 2022 07:36:59 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=sRqIbgNC; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C6F771E143 for ; Wed, 18 May 2022 07:36:58 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 328043857363 for ; Wed, 18 May 2022 11:36:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 328043857363 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1652873818; bh=yw4cVGC/krBiMobRuIEfSYEa8u+dQdWoji3pGlypd44=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=sRqIbgNCsRguRU3FIt4Qv1UBvBJ6dspWT8Kc6qdXPEMVs4PgS6h6OWaj2eLXKoMRx xesh83hMZ5RvRqck70/P4sCTQYYe3JrVMtsChZGixSreq5MBb0rNA9drV7vV6EX/wR tNVriHMgetihvWbiK1phP6i3ak76F+95LEPwGObk= Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 8F9323858C52 for ; Wed, 18 May 2022 11:36:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8F9323858C52 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id C3EDA21B5F; Wed, 18 May 2022 11:36:38 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A867613A6D; Wed, 18 May 2022 11:36:38 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id uYrsJ0bahGIEVAAAMHmgww (envelope-from ); Wed, 18 May 2022 11:36:38 +0000 Date: Wed, 18 May 2022 13:36:36 +0200 To: gdb-patches@sourceware.org Subject: [PATCH][gdb/fortran] Fix literal truncation Message-ID: <20220518113635.GA22588@delia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Tom de Vries via Gdb-patches Reply-To: Tom de Vries Cc: Andrew Burgess , Pedro Alves Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Hi, As mentioned in commit 5b758627a18 ("Make gdb.base/parse_number.exp test all architectures"): ... There might be a bug that 32-bit fortran truncates 64-bit values to 32-bit, given "p/x 0xffffffffffffffff" returns "0xffffffff". ... 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 = 0xffffffffffffffff The target architecture is set to "i386". $1 = 0xffffffff ... Fix this by adding a range check in parse_number in gdb/f-exp.y. Tested on x86_64-linux. Any comments? Thanks, - Tom [gdb/fortran] Fix literal truncation --- gdb/f-exp.y | 16 ++++++++++------ gdb/testsuite/gdb.base/parse_number.exp | 24 +++++++++++++++--------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/gdb/f-exp.y b/gdb/f-exp.y index 90cc2c65c7b..908ba2cb071 100644 --- a/gdb/f-exp.y +++ b/gdb/f-exp.y @@ -1100,7 +1100,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. */ - + + int bits_available; if ((gdbarch_int_bit (par_state->gdbarch ()) != gdbarch_long_bit (par_state->gdbarch ()) && ((n >> 2) @@ -1108,19 +1109,22 @@ parse_number (struct parser_state *par_state, shift warning */ || long_p) { - high_bit = ((ULONGEST)1) - << (gdbarch_long_bit (par_state->gdbarch ())-1); + bits_available = gdbarch_long_bit (par_state->gdbarch ()); unsigned_type = parse_type (par_state)->builtin_unsigned_long; signed_type = parse_type (par_state)->builtin_long; - } + } else { - high_bit = - ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1); + bits_available = gdbarch_int_bit (par_state->gdbarch ()); unsigned_type = parse_type (par_state)->builtin_unsigned_int; signed_type = parse_type (par_state)->builtin_int; } + high_bit = ((ULONGEST)1) << (bits_available - 1); + if (RANGE_CHECK + && ((n >> 2) >> (bits_available - 2))) + range_error (_("Overflow on numeric constant.")); + putithere->typed_val.val = n; /* 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.base/parse_number.exp index 444f5d0534b..4439875837f 100644 --- a/gdb/testsuite/gdb.base/parse_number.exp +++ b/gdb/testsuite/gdb.base/parse_number.exp @@ -47,16 +47,10 @@ proc test_parse_numbers {arch} { if {$sizeof_long_long == 8 && $sizeof_long == 8} { set 8B_type "unsigned long" - set fortran_type "unsigned long" - set fortran_value "0xffffffffffffffff" } elseif {$sizeof_long_long == 8 && $sizeof_long == 4 && $sizeof_int == 4} { set 8B_type "unsigned long long" - set fortran_type "unsigned int" - set fortran_value "0xffffffff" } elseif {$sizeof_long == 4 && $sizeof_int == 2} { set 8B_type "unsigned long long" - set fortran_type "unsigned long" - set fortran_value "0xffffffff" } else { error "missing case for long long = $sizeof_long_long, long = $sizeof_long, int = $sizeof_int" } @@ -68,13 +62,25 @@ proc test_parse_numbers {arch} { gdb_test_no_output "set language $lang" + if { $lang == "modula-2" || $lang == "fortran" } { + set re_overflow "Overflow on numeric constant\\." + } elseif { $lang == "ada" } { + set re_overflow "Integer literal out of range" + } else { + set re_overflow "Numeric constant too large\\." + } + set val "0xffffffffffffffff" set val [hex_for_lang $lang $val] if {$lang == "fortran"} { - gdb_test "p/x $val" " = $fortran_value" - gdb_test "ptype $val" " = $fortran_type" + if { $sizeof_long == 4 } { + gdb_test "p/x $val" $re_overflow + } else { + gdb_test "p/x $val" " = $val" + gdb_test "ptype $val" " = unsigned long" + } } elseif {$lang == "modula-2"} { - gdb_test "p/x $val" "Overflow on numeric constant\\." + gdb_test "p/x $val" $re_overflow } else { # D and Rust define their own built-in 64-bit types, and # are thus always able to parse/print 64-bit values.