public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/114325] New: std::format gives incorrect results for negative numbers
@ 2024-03-13 14:02 luigighiron at gmail dot com
2024-03-13 14:42 ` [Bug libstdc++/114325] " mwd at md5i dot com
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: luigighiron at gmail dot com @ 2024-03-13 14:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114325
Bug ID: 114325
Summary: std::format gives incorrect results for negative
numbers
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: luigighiron at gmail dot com
Target Milestone: ---
The following code generates an incorrect result with libstdc++:
std::format("{}",-100)
From testing on godbolt this seems to generate the string "-1\0000", then when
printed it looks like -10. This seems exclusive to GCC 14, and happens for any
numbers less than -99.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug libstdc++/114325] std::format gives incorrect results for negative numbers
2024-03-13 14:02 [Bug libstdc++/114325] New: std::format gives incorrect results for negative numbers luigighiron at gmail dot com
@ 2024-03-13 14:42 ` mwd at md5i dot com
2024-03-13 19:45 ` [Bug libstdc++/114325] [14 Regression] " redi at gcc dot gnu.org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: mwd at md5i dot com @ 2024-03-13 14:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114325
Michael Duggan <mwd at md5i dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |mwd at md5i dot com
--- Comment #1 from Michael Duggan <mwd at md5i dot com> ---
I will note that, in experiments, this seems to solely happen with "{}". If
anything else is in the format string, it works correctly. This is probably a
bug in the fairly recent codepath that optimizes the "{}" case.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug libstdc++/114325] [14 Regression] std::format gives incorrect results for negative numbers
2024-03-13 14:02 [Bug libstdc++/114325] New: std::format gives incorrect results for negative numbers luigighiron at gmail dot com
2024-03-13 14:42 ` [Bug libstdc++/114325] " mwd at md5i dot com
@ 2024-03-13 19:45 ` redi at gcc dot gnu.org
2024-03-13 19:52 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-13 19:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114325
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Known to work| |13.2.1
Target Milestone|--- |14.0
Last reconfirmed| |2024-03-13
Ever confirmed|0 |1
Status|UNCONFIRMED |ASSIGNED
Summary|std::format gives incorrect |[14 Regression] std::format
|results for negative |gives incorrect results for
|numbers |negative numbers
Known to fail| |14.0
Assignee|unassigned at gcc dot gnu.org |redi at gcc dot gnu.org
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug libstdc++/114325] [14 Regression] std::format gives incorrect results for negative numbers
2024-03-13 14:02 [Bug libstdc++/114325] New: std::format gives incorrect results for negative numbers luigighiron at gmail dot com
2024-03-13 14:42 ` [Bug libstdc++/114325] " mwd at md5i dot com
2024-03-13 19:45 ` [Bug libstdc++/114325] [14 Regression] " redi at gcc dot gnu.org
@ 2024-03-13 19:52 ` redi at gcc dot gnu.org
2024-03-14 16:58 ` cvs-commit at gcc dot gnu.org
2024-03-14 17:02 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-13 19:52 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114325
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Indeed. Here's the fix:
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -4124,14 +4124,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;
}
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug libstdc++/114325] [14 Regression] std::format gives incorrect results for negative numbers
2024-03-13 14:02 [Bug libstdc++/114325] New: std::format gives incorrect results for negative numbers luigighiron at gmail dot com
` (2 preceding siblings ...)
2024-03-13 19:52 ` redi at gcc dot gnu.org
@ 2024-03-14 16:58 ` cvs-commit at gcc dot gnu.org
2024-03-14 17:02 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-14 16:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114325
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:
https://gcc.gnu.org/g:f89cfdb2f2e9b4fe517b1e00511c4d70a7014cbc
commit r14-9479-gf89cfdb2f2e9b4fe517b1e00511c4d70a7014cbc
Author: Jonathan Wakely <jwakely@redhat.com>
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug libstdc++/114325] [14 Regression] std::format gives incorrect results for negative numbers
2024-03-13 14:02 [Bug libstdc++/114325] New: std::format gives incorrect results for negative numbers luigighiron at gmail dot com
` (3 preceding siblings ...)
2024-03-14 16:58 ` cvs-commit at gcc dot gnu.org
@ 2024-03-14 17:02 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-14 17:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114325
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|ASSIGNED |RESOLVED
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for GCC 14, thanks for the report.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-03-14 17:02 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-13 14:02 [Bug libstdc++/114325] New: std::format gives incorrect results for negative numbers luigighiron at gmail dot com
2024-03-13 14:42 ` [Bug libstdc++/114325] " mwd at md5i dot com
2024-03-13 19:45 ` [Bug libstdc++/114325] [14 Regression] " redi at gcc dot gnu.org
2024-03-13 19:52 ` redi at gcc dot gnu.org
2024-03-14 16:58 ` cvs-commit at gcc dot gnu.org
2024-03-14 17:02 ` redi at gcc dot gnu.org
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).