From patchwork Mon Aug 14 09:57:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Dreik X-Patchwork-Id: 135297 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2633097vqi; Mon, 14 Aug 2023 02:57:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6z/Ly3P2VvConXi3+xxy0ldVlU1hjbVdFMSnKoZBpEAJU/YHA2yXmGDYCPvC9vS6s9OLc X-Received: by 2002:a17:906:73ca:b0:973:d1ce:dbe8 with SMTP id n10-20020a17090673ca00b00973d1cedbe8mr7087525ejl.46.1692007078368; Mon, 14 Aug 2023 02:57:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692007078; cv=none; d=google.com; s=arc-20160816; b=kvAAJyTn0n1oT2fNnYQFl0U20lJnDUUyXF+WfVdPWfEfzILewiZrdCNWXKdTJiDvHU XtyLnub5Cka6ZwtNbiT6hI7ZMtpkFjDGW8DdLW++yJvbC09toRA/+Edu8UQBRN9Ur+hm nysEusE+aB56BurNeP7I4pW8aeGd7k0O7VKeCbZnIucBvco/lDZ4zO5qnMopOLYu1F0S te1ftIU16GY8AM2wgmgXJAVrYBi5VLM9fZP4ZDDsSXSzOGUazTVU3jBSjBn/bz16Dp/t ujULK1724RMJbTVTasyczxVs3fatyMxZlmdHlgrN76PAonN2iJLa1FTVsjnLK4qdbKAB HvIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:cc:to :content-language:user-agent:mime-version:date:message-id :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=K3cIFk9TvgJVl8SFIhVVgI41OZiXcwjpQgxYAJFC234=; fh=dU/7qDSCvCC+nnZQeUTiJO0/zE0zklOlr0YTlvyCabs=; b=fU8D4xqvtPDMw5KF2/MCuGGHxA3w434XwbZ3Kfyw6zlLdPJnHPFMw91IEWYUkufeno whTuPVNySHqu08+XKt7cpKPSMQBSJwIlskh3QqvWk9uQ04cfnc7Ci4YnbF6FmdbS48Et 9O3HAezBi8VMrW+Z19wTISttrGM80NgwswdPCcaIm/fcteDlyjjV2dGOebT7vAExFu8r LWPiZCI12pZX2TS+P6jPoqce47Im9uWvE5Vk+I0Y3sL4DtZD3CYImxEQPJ/LNy2s1h5c Yx2k3dFh3J3HGGUhgtYzQQ6BINsB/52fqqLleiRC6f6joNS1pnb9tWD9eBaNpJRGRmYM s1nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="w+N/TZl0"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id l17-20020a1709060e1100b0099ccdd7770fsi8007123eji.995.2023.08.14.02.57.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 02:57:58 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="w+N/TZl0"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 53492385841D for ; Mon, 14 Aug 2023 09:57:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53492385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692007077; bh=K3cIFk9TvgJVl8SFIhVVgI41OZiXcwjpQgxYAJFC234=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=w+N/TZl0UGPNuVTm9E6qsnlWowoBNzDKhG1LPzzThfQjM1DcJtxEYmmLJiW3E/lqe hLCJTnHwRru5vIUME+aYWLXTvXmU4GR6WzJhHJ4+AiYYPJTCiNheOQjxSmC1qP5PQ3 i123l7nwQHtXJqO+6N9FwHaFpMEbZaDzp5qzGl3U= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from ts201-smtpout74.ddc.teliasonera.net (ts201-smtpout74.ddc.teliasonera.net [81.236.60.182]) by sourceware.org (Postfix) with ESMTP id D53FB3858D39; Mon, 14 Aug 2023 09:57:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D53FB3858D39 X-RG-Rigid: 64171A3C065B0AFE X-Originating-IP: [90.231.147.135] X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedviedruddtgedgvdduucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuvffgnffktefuhgdpggftfghnshhusghstghrihgsvgdpqfgfvfenuceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfvhfevufgtsehgtderredtfeejnecuhfhrohhmpefrrghulhcuffhrvghikhcuoehgtggtphgrthgthhgvshesphgruhhlughrvghikhdrshgvqeenucggtffrrghtthgvrhhnpeffvefhfeeigfegvdduhfeuveejhfeijeeftdehhfegheejvddvtdduieelhfeiueenucffohhmrghinhepghhnuhdrohhrghenucfkphepledtrddvfedurddugeejrddufeehnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghlohepmhgrihhlshgvrhhvvghrrdgurhgvihhkrdhsvgdpihhnvghtpeeltddrvdefuddrudegjedrudefhedpmhgrihhlfhhrohhmpehgtggtphgrthgthhgvshesphgruhhlughrvghikhdrshgvpdhnsggprhgtphhtthhopedvpdhrtghpthhtohepghgttgdqphgrthgthhgvshesghgttgdrghhnuhdrohhrghdprhgtphhtthhopehlihgsshhtuggtoddosehgtggtrdhgnhhurdhorhhg X-RazorGate-Vade-Verdict: clean 0 X-RazorGate-Vade-Classification: clean Received: from mailserver.dreik.se (90.231.147.135) by ts201-smtpout74.ddc.teliasonera.net (5.8.716) id 64171A3C065B0AFE; Mon, 14 Aug 2023 11:57:10 +0200 Received: from [192.168.122.88] (OpenWrt.lan [192.168.1.1]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mailserver.dreik.se (Postfix) with ESMTPSA id 9B29223EF3A; Mon, 14 Aug 2023 11:57:10 +0200 (CEST) Message-ID: <3311a355-15c8-4cdb-1644-b52d8aecbd63@pauldreik.se> Date: Mon, 14 Aug 2023 11:57:09 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Content-Language: en-US To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org Subject: [PATCH] Fix for bug libstdc++/110860 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Paul Dreik via Gcc-patches From: Paul Dreik Reply-To: Paul Dreik Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774198013980644307 X-GMAIL-MSGID: 1774198013980644307 The patch below fixes an issue with the fix already committed for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110860 which unfortunately was not sufficient for small magnitude floating point values. With the patch in place, the code now survives the fuzzing I used to find the problem in the first place. Tested on amd64. I prepared the patch using git show, which should include the signoff as instructed per the DCO. Thanks, Paul ------------------------------------------------------------------------ commit 848b8d948787495e64ed9c55d681eccf730b74fb Author: Paul Dreik Date: Mon Aug 14 11:52:30 2023 +0200 libstdc++: Avoid problematic use of log10 in std::format [PR110860] If abs(__v) is smaller than one, the result will be on the form 0.xxxxx. It is only if the magnitude is large that more digits are needed before the decimal dot. This uses frexp instead of log10 which should be less expensive and have sufficient precision for the desired purpose. It removes the problematic cases where log10 will be negative or not fit in an int. Signed-off-by: Paul Dreik } diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index f4520ff3f..729e3d4b9 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -1490,14 +1490,22 @@ namespace __format // If the buffer is too small it's probably because of a large // precision, or a very large value in fixed format. size_t __guess = 8 + __prec; - if (__fmt == chars_format::fixed && __v != 0) // +ddd.prec + if (__fmt == chars_format::fixed) // +ddd.prec { - if constexpr (is_same_v<_Fp, float>) - __guess += __builtin_log10f(__v < 0.0f ? -__v : __v); - else if constexpr (is_same_v<_Fp, double>) - __guess += __builtin_log10(__v < 0.0 ? -__v : __v); - else if constexpr (is_same_v<_Fp, long double>) - __guess += __builtin_log10l(__v < 0.0l ? -__v : __v); + if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double> || is_same_v<_Fp, long double>) + { + // the number of digits to the left of the decimal point + // is floor(log10(max(abs(__v),1)))+1 + int __exp{}; + if constexpr (is_same_v<_Fp, float>) + __builtin_frexpf(__v, &__exp); + else if constexpr (is_same_v<_Fp, double>) + __builtin_frexp(__v, &__exp); + else if constexpr (is_same_v<_Fp, long double>) + __builtin_frexpl(__v, &__exp); + if (__exp>0) + __guess += 1U + __exp * 4004U / 13301U; // log10(2) approx. + } else __guess += numeric_limits<_Fp>::max_exponent10;