public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix std::format("{}", negative_integer) [PR114325]
Date: Thu, 14 Mar 2024 17:00:52 +0000	[thread overview]
Message-ID: <20240314170109.2835409-1-jwakely@redhat.com> (raw)

Tested aarch64-linux. Pushed to trunk.

-- >8 --

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.
---
 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");
   }
 }
 
-- 
2.44.0


                 reply	other threads:[~2024-03-14 17:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240314170109.2835409-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).