From 7f157dca0a7d98709b5b7ee4b4792bda9c05b19d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 22 Jul 2024 10:31:13 -0700 Subject: [PATCH] Workaround gcc stringop-overflow bug --- include/fmt/format.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index a84fc717..18a009d8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1295,23 +1295,23 @@ template FMT_CONSTEXPR20 auto format_decimal(Char* out, UInt value, int size) -> format_decimal_result { FMT_ASSERT(size >= count_digits(value), "invalid digit count"); - out += size; - Char* end = out; + auto begin = out, end = out + size; + unsigned n = to_unsigned(size); while (value >= 100) { // Integer division is slow so do it for a group of two digits instead // of for every digit. The idea comes from the talk by Alexandrescu // "Three Optimization Tips for C++". See speed-test for a comparison. - out -= 2; - copy2(out, digits2(static_cast(value % 100))); + n -= 2; + copy2(begin + n, digits2(static_cast(value % 100))); value /= 100; } - if (value < 10) { - *--out = static_cast('0' + value); - return {out, end}; + if (value >= 10) { + n -= 2; + copy2(begin + n, digits2(static_cast(value))); + } else { + begin[--n] = static_cast('0' + value); } - out -= 2; - copy2(out, digits2(static_cast(value))); - return {out, end}; + return {begin + n, end}; } template