From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 9DAFC383F942 for ; Mon, 23 May 2022 13:06:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9DAFC383F942 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-out2.suse.de (Postfix) with ESMTPS id D9E071F904 for ; Mon, 23 May 2022 13:06:11 +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 C512D13AA5 for ; Mon, 23 May 2022 13:06:11 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id eLgDL8OGi2LGTwAAMHmgww (envelope-from ) for ; Mon, 23 May 2022 13:06:11 +0000 Message-ID: Date: Mon, 23 May 2022 15:06:11 +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 2/2] [gdbsupport] Fix UB in print-utils.cc:int_string Content-Language: en-US To: gdb-patches@sourceware.org References: <20220517154048.13213-1-tdevries@suse.de> <20220517154048.13213-2-tdevries@suse.de> From: Tom de Vries In-Reply-To: <20220517154048.13213-2-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:06:14 -0000 On 5/17/22 17:40, Tom de Vries via Gdb-patches wrote: > When building gdb with -fsanitize=undefined, I run into: > ... > (gdb) PASS: gdb.ada/access_to_packed_array.exp: set logging enabled on > maint print symbols^M > print-utils.cc:281:29:runtime error: negation of -9223372036854775808 cannot \ > be represented in type 'long int'; cast to an unsigned type to negate this \ > value to itself > (gdb) FAIL: gdb.ada/access_to_packed_array.exp: maint print symbols > ... > > By running in a debug session, we find that this happens during printing of: > ... > typedef system.storage_elements.storage_offset: \ > range -9223372036854775808 .. 9223372036854775807; > ... > Possibly, an ada test-case could be created that exercises this in isolation. > > The problem is here in int_string, where we negate a val with type LONGEST: > ... > return decimal2str ("-", -val, width); > ... > > Fix this by, as recommend, using "-(ULONGEST)val" instead. > > Tested on x86_64-linux. Committed. Thanks, - Tom > --- > gdbsupport/print-utils.cc | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/gdbsupport/print-utils.cc b/gdbsupport/print-utils.cc > index 73ff1afda30..7bbb6deea74 100644 > --- a/gdbsupport/print-utils.cc > +++ b/gdbsupport/print-utils.cc > @@ -278,7 +278,11 @@ int_string (LONGEST val, int radix, int is_signed, int width, > case 10: > { > if (is_signed && val < 0) > - return decimal2str ("-", -val, width); > + /* Cast to unsigned before negating, to prevent runtime error: > + negation of -9223372036854775808 cannot be represented in type > + 'long int'; cast to an unsigned type to negate this value to > + itself. */ > + return decimal2str ("-", -(ULONGEST)val, width); > else > return decimal2str ("", val, width); > }