From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 30DC3385770F; Thu, 14 Mar 2024 16:58:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 30DC3385770F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710435526; bh=jbBLDz/3BtGZhBWScn2IvYJ74xhtTXIW45aod6DGEsc=; h=From:To:Subject:Date:From; b=rzovVqoWPzmmMBv5R1r3qch2r530jd6o6JQZPWFsQvUI+khaoOWy/hSzmjEWbg+qh zqIl3ZK/YlWSQXZyJF4tLKZXxCQWUEUaTpRPMARoNnwtVbiWyEyHUSr2SIbKR4NOC1 Aa33GAztAeQxUPeufx3OpfHLHAmCEpbbiLfayyoQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-9479] libstdc++: Fix std::format("{}", negative_integer) [PR114325] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: df483ebd24689a3bebfae2089637a00eca0e5a12 X-Git-Newrev: f89cfdb2f2e9b4fe517b1e00511c4d70a7014cbc Message-Id: <20240314165846.30DC3385770F@sourceware.org> Date: Thu, 14 Mar 2024 16:58:46 +0000 (GMT) List-Id: https://gcc.gnu.org/g:f89cfdb2f2e9b4fe517b1e00511c4d70a7014cbc commit r14-9479-gf89cfdb2f2e9b4fe517b1e00511c4d70a7014cbc Author: Jonathan Wakely Date: Wed Mar 13 21:19:54 2024 +0000 libstdc++: Fix std::format("{}", negative_integer) [PR114325] The fast path for "{}" format strings has a bug for negative integers where the length passed to std::to_chars is too long. libstdc++-v3/ChangeLog: PR libstdc++/114325 * include/std/format (_Scanner::_M_scan): Pass correct length to __to_chars_10_impl. * testsuite/std/format/functions/format.cc: Check negative integers with empty format-spec. Diff: --- libstdc++-v3/include/std/format | 7 ++++--- libstdc++-v3/testsuite/std/format/functions/format.cc | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 1e839e88db4..613016d1a10 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -4091,6 +4091,7 @@ namespace __format __sink_out = __sink.out(); if constexpr (is_same_v<_CharT, char>) + // Fast path for "{}" format strings and simple format arg types. if (__fmt.size() == 2 && __fmt[0] == '{' && __fmt[1] == '}') { bool __done = false; @@ -4124,14 +4125,14 @@ namespace __format __uval = make_unsigned_t<_Tp>(~__arg) + 1u; else __uval = __arg; - const auto __n = __detail::__to_chars_len(__uval) + __neg; - if (auto __res = __sink_out._M_reserve(__n)) + const auto __n = __detail::__to_chars_len(__uval); + if (auto __res = __sink_out._M_reserve(__n + __neg)) { auto __ptr = __res.get(); *__ptr = '-'; __detail::__to_chars_10_impl(__ptr + (int)__neg, __n, __uval); - __res._M_bump(__n); + __res._M_bump(__n + __neg); __done = true; } } diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index a27fbe74631..4499397aaf9 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -157,6 +157,11 @@ test_std_examples() // Restore std::locale::global(std::locale::classic()); + + string s5 = format("{}", -100); // PR libstdc++/114325 + VERIFY(s5 == "-100"); + string s6 = format("{:d} {:d}", -123, 999); + VERIFY(s6 == "-123 999"); } }