From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id kduRKrLBomo/9DwAWB0awg (envelope-from ) for ; Thu, 10 Sep 2026 10:41:54 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=SeEfC67f; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=HJ1d6Yra; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=NuCQMWfk; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=8iOprXgS; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 98AE01E09E; Thu, 10 Sep 2026 10:41:54 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 86B411E091 for ; Thu, 10 Sep 2026 10:41:52 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0F7104BB24C9 for ; Thu, 10 Sep 2026 14:41:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0F7104BB24C9 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=SeEfC67f; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=HJ1d6Yra; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=NuCQMWfk; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=8iOprXgS Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 6DE264BA79B1 for ; Thu, 10 Sep 2026 14:41:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6DE264BA79B1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6DE264BA79B1 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789051286; cv=none; b=Mo/Yo4/rQTQbDA2QOAfWcisCFsqsr9+lSfx9Hd88LO4VfpNNGKdUT0l870xswbBGF1B8Hpgott+8i3WVM7Qino0LJKD0RzUP7DS0u77pGbc/PZ1uErtX0QAadJ1F/dQRKJbkZAIm5bUJMSxUKPYwsz6oYsYksvUXgnJh9I88EOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789051286; c=relaxed/simple; bh=kcaQrPVsm4E9HLPNzp4yZM6j4UZ/8XWBg24znaELBkQ=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature: Message-ID:Date:MIME-Version:Subject:To:From; b=Adns3bhdZwYclZvN9xtwH47MFva+eJM9ShSRVBgTblzDINZvumNtxtWxvOr47NV8az+52stm5li296m9ev49GHLwhPDczibuX5R7US6Mle1ha655wqDMofcaiM+u8Fu7/2pv4I2BorDxLU4xytONYmO3QyORA4KdAhR1aMVLPi4= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=SeEfC67f; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=HJ1d6Yra; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=NuCQMWfk; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=8iOprXgS DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6DE264BA79B1 Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4BC8621D55; Thu, 10 Sep 2026 14:41:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1789051281; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iRwwr3jrSB3jqMpUazR3lIql//13CH/SuDkYScygAd8=; b=SeEfC67fWNbXg7gjX2xiQHyBkB+JVCD5ApPqc3jvQ8WDDEQ17EO4VjnQTRDqA/60AMzDwT rFn70jGM1nROlOinLEdpXXjFOz0zeYB/umM8O7F8PZpJcdMZh5U8A395x7VFCE6RFM5doR 0SPCM4VtGtFg7P7rrk3+ernZOjkuM0U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1789051281; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iRwwr3jrSB3jqMpUazR3lIql//13CH/SuDkYScygAd8=; b=HJ1d6YraX2uI8O6ZXxR0MzfVf/ar1seiUVYEeD4PYkd/HbtWNu3p4/kNpEfAyEIxUKZj+l Z5w1AGGxdFIz/0Bw== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1789051277; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iRwwr3jrSB3jqMpUazR3lIql//13CH/SuDkYScygAd8=; b=NuCQMWfk+eEdnQSQJrtPeHSpuh0UdBiXgJ5SCf45kj/+PejZNRWPVY7S5z0QDbPE7L3NbL vglJ6FhqZP4fEryGx2IZtOhNywDVclZqCe5K3RfbMOib/EErRrUnl2Ry4wBhPpzyWXHvfC yCDoVogXlIq1yMKyVDMRc9kdmaYpqqg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1789051277; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iRwwr3jrSB3jqMpUazR3lIql//13CH/SuDkYScygAd8=; b=8iOprXgSbK8EP4PO6f/evKV9/g8/6KgrYkOreHbB0DgO/rJ++CGELIUm52/FFg3OWHfMmW QB+0Bj606hrgw2Cw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 1618A13180; Thu, 10 Sep 2026 14:41:16 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id eDQkOYzBomoKXgAAD6G6ig (envelope-from ); Thu, 10 Sep 2026 14:41:16 +0000 Message-ID: Date: Thu, 10 Sep 2026 16:41:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] Rewrite gdb_mpz::export_bits To: Tom Tromey , gdb-patches@sourceware.org References: <20260909180822.2255847-1-tromey@adacore.com> Content-Language: en-US From: Tom de Vries In-Reply-To: <20260909180822.2255847-1-tromey@adacore.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-0.995]; MIME_GOOD(-0.10)[text/plain]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; MID_RHS_MATCH_FROM(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[sourceware.org:url, buf.data:url, imap1.dmz-prg2.suse.org:helo, suse.de:mid, suse.de:email] X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org On 9/9/26 8:08 PM, Tom Tromey wrote: Hi Tom, thanks for fixing this. This LGTM. I have some comments below, but these are all nits, so feel free to ignore. Approved-By: Tom de Vries > A couple of bugs point out that, when multiplication overflows, gdb > does not compute the correct result. > This is caused by some bugs in > gdb_mpz::export_bits. There's more than one bug? If not, then maybe use "a bug". > This patch rewrites part of this function, > hopefully now getting the correct answer. I think the new code should > be somewhat simpler to follow. > Agreed, it is simpler. > A new selftest is added, derived from the code in the bug report. > > This rewrite doesn't try to minimize allocations, the way the previous > one did. I tend to doubt that matters, and this is one of the > readability improvements IMO. > Agreed. I was not familiar with this code, so I ended up splitting the patch in three: - drop allocation minimization - fix - rename exported_val to masked to get a more minimal fix to look at. I could live without the third patch, or a more neutral name like res or tmp. Another approach could be to use meaningful names like masked, but then do something like: ... - masked += neg_offset; - } + un_signed = masked + neg_offset + } else { + un_signed = masked; + } ... If so, I prefer "truncated" to "masked". > Regression tested on x86-64 Fedora 43. > > I am not sure but it might be worth applying this to gdb 18; your > thoughts appreciated. > I think it's worth it. As mentioned, I'm not familiar with this code, so I can't asses the risk. > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34601 > --- > gdb/gmp-utils.c | 38 ++++++++++------------------- > gdb/unittests/gmp-utils-selftests.c | 6 +++++ > 2 files changed, 19 insertions(+), 25 deletions(-) > > diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c > index b7fed9a82d1..949d75d21bd 100644 > --- a/gdb/gmp-utils.c > +++ b/gdb/gmp-utils.c > @@ -128,35 +128,22 @@ gdb_mpz::export_bits (gdb::array_view buf, int endian, bool unsigned_p > hi.str ().c_str ()); > } > > - const gdb_mpz *exported_val = this; > - gdb_mpz un_signed; > - if (sign < 0) > + gdb_mpz masked = *this; > + masked.mask (buf.size () * HOST_CHAR_BIT); > + > + if (sign < 0 && masked.sgn () != 0) I think it's clearer to do this, because there's less state to keep going forward: ... if (masked.sgn () == 0) { memset (buf.data (), 0, buf.size ()); return; } if (sign < 0) ... This starts to get verbose, but after factoring out some lambda functions: ... + auto export_zero = [&] () + { + memset (buf.data (), 0, buf.size ()); + }; + + auto export_if_zero = [&] (gdb_mpz &val) + { + if (val.sgn () != 0) + return false; + + export_zero (); + return true; + }; ... this is reduced from 5 to 2 lines: ... if (export_if_zero (masked)) return; if (sign < 0) ... Thanks, - Tom > { > /* mpz_export does not handle signed values, so create a positive > value whose bit representation as an unsigned of the same length > - would be the same as our negative value. */ > + would be the same as our negative value. However, if masking > + left us with 0, we don't need to do anything else. */ > gdb_mpz neg_offset = gdb_mpz::pow (2, buf.size () * HOST_CHAR_BIT); > - un_signed = *exported_val + neg_offset; > - exported_val = &un_signed; > - } > - > - /* If the value is too large, truncate it. */ > - if (!safe > - && mpz_sizeinbase (exported_val->m_val, 2) > buf.size () * HOST_CHAR_BIT) > - { > - /* If we don't already have a copy, make it now. */ > - if (exported_val != &un_signed) > - { > - un_signed = *exported_val; > - exported_val = &un_signed; > - } > - > - un_signed.mask (buf.size () * HOST_CHAR_BIT); > + masked += neg_offset; > } > > - /* It's possible that one of the above results in zero, which has to > - be handled specially. */ > - if (exported_val->sgn () == 0) > + /* It's possible that the above results in zero, which has to be > + handled specially. */ > + if (masked.sgn () == 0) > { > memset (buf.data (), 0, buf.size ()); > return; > @@ -174,8 +161,9 @@ gdb_mpz::export_bits (gdb::array_view buf, int endian, bool unsigned_p > > size_t word_countp; > gdb::unique_xmalloc_ptr exported > - (mpz_export (NULL, &word_countp, -1 /* order */, buf.size () /* size */, > - endian, 0 /* nails */, exported_val->m_val)); > + (mpz_export (nullptr, &word_countp, -1 /* order */, > + buf.size () /* size */, endian, 0 /* nails */, > + masked.m_val)); > > gdb_assert (word_countp == 1); > > diff --git a/gdb/unittests/gmp-utils-selftests.c b/gdb/unittests/gmp-utils-selftests.c > index 1912d346c17..417abae5463 100644 > --- a/gdb/unittests/gmp-utils-selftests.c > +++ b/gdb/unittests/gmp-utils-selftests.c > @@ -80,6 +80,12 @@ gdb_mpz_as_integer () > v -= 1; > > SELF_CHECK (v.as_integer () == ul_expected); > + > + /* This is from PR gdb/34601. */ > + LONGEST neg = (LONGEST) 0x8000000000000001ull; > + gdb_mpz a (neg); > + gdb_mpz b (0x1234); > + SELF_CHECK ((a * b).as_integer_truncate () == 0x1234); > } > > /* A helper function which calls the given gdb_mpz object's as_integer > > base-commit: 2ece447a4f3dcf1d96bcaca644dbf895e42194b8