From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id D42173857340 for ; Mon, 23 May 2022 13:04:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D42173857340 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 18D0621A8D for ; Mon, 23 May 2022 13:04:47 +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 08E0113AA5 for ; Mon, 23 May 2022 13:04:47 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id LogUAW+Gi2IaTwAAMHmgww (envelope-from ) for ; Mon, 23 May 2022 13:04:47 +0000 Message-ID: <126a9084-92b5-ff90-808b-152ea41db04a@suse.de> Date: Mon, 23 May 2022 15:04:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: [committed][PATCH 1/2] [gdb/exp] Fix UB in scalar_binop Content-Language: en-US To: gdb-patches@sourceware.org References: <20220517154048.13213-1-tdevries@suse.de> From: Tom de Vries In-Reply-To: <20220517154048.13213-1-tdevries@suse.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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: , X-List-Received-Date: Mon, 23 May 2022 13:04:49 -0000 On 5/17/22 17:40, Tom de Vries via Gdb-patches wrote: > When building gdb with -fsanitize=undefined, I run into: > ... > $ gdb -q -batch -ex "p -(-0x7fffffffffffffff - 1)" > src/gdb/valarith.c:1385:10: runtime error: signed integer overflow: \ > 0 - -9223372036854775808 cannot be represented in type 'long int' > $1 = -9223372036854775808 > ... > > Fix this by performing the substraction in scalar_binop using unsigned types. > > Tested on x86_64-linux. Committed. Thanks, - Tom > --- > gdb/testsuite/gdb.base/arithmet.exp | 2 ++ > gdb/valarith.c | 5 ++++- > 2 files changed, 6 insertions(+), 1 deletion(-) > > diff --git a/gdb/testsuite/gdb.base/arithmet.exp b/gdb/testsuite/gdb.base/arithmet.exp > index b6009a36235..4905c2e2706 100644 > --- a/gdb/testsuite/gdb.base/arithmet.exp > +++ b/gdb/testsuite/gdb.base/arithmet.exp > @@ -98,3 +98,5 @@ gdb_test "print x-(y+w)" "3" > gdb_test "print x/(y*w)" "0" > gdb_test "print x-(y/w)" "9" > gdb_test "print (x+y)*w" "42" > + > +gdb_test "p /x -(-0x7fffffffffffffff - 1)" " = 0x8000000000000000" > diff --git a/gdb/valarith.c b/gdb/valarith.c > index 6210267826e..526cc02599e 100644 > --- a/gdb/valarith.c > +++ b/gdb/valarith.c > @@ -1382,7 +1382,10 @@ scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) > break; > > case BINOP_SUB: > - v = v1 - v2; > + /* Avoid runtime error: signed integer overflow: \ > + 0 - -9223372036854775808 cannot be represented in type > + 'long int'. */ > + v = (ULONGEST)v1 - (ULONGEST)v2; > break; > > case BINOP_MUL: > > base-commit: a1f2ddd38378c8db63e75daa28b7e304c2fd774f