public inbox for
 help / color / mirror / Atom feed
* [committed] libstdc++: Do not call log10(0.0) in std::format [PR110860]
@ 2023-08-11 17:21 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-08-11 17:21 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Second attempt to fix this PR. Tested x86_64-linux, pushed to trunk.

-- >8 --

Calling log10(0.0) returns -inf which has undefined behaviour when
converted to an integer. We only need to use log10 for large values
anyway. If the value is zero then the larger buffer is only needed due
to a large precision, so we don't need to use log10 to estimate the
number of digits for the significand.


	PR libstdc++/110860
	* include/std/format (__formatter_fp::format): Do not call log10
	with zero values.
 libstdc++-v3/include/std/format | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 2fe430f75f6..23da6b008c5 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -1490,7 +1490,7 @@ namespace __format
 	      // If the buffer is too small it's probably because of a large
 	      // precision, or a very large value in fixed format.
 	      size_t __guess = 8 + __prec;
-	      if (__fmt == chars_format::fixed) // +ddd.prec
+	      if (__fmt == chars_format::fixed && __v != 0) // +ddd.prec
 		  if constexpr (is_same_v<_Fp, float>)
 		    __guess += __builtin_log10f(__v < 0.0f ? -__v : __v);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-08-11 17:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-11 17:21 [committed] libstdc++: Do not call log10(0.0) in std::format [PR110860] Jonathan Wakely

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).