[Bug libstdc++/114325] [14 Regression] std::format gives incorrect results for negative numbers
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Mar 13 19:52:38 GMT 2024
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;
}
}
More information about the Gcc-bugs
mailing list