From 708fff700f36ab3c2ab107b984ec9f3b8be5f055 Mon Sep 17 00:00:00 2001 From: Mikael Persson Date: Tue, 10 Jun 2025 20:45:26 -0400 Subject: Fixed undefined behavior with log10 cast of fractional doubles (#276) --- include/toml++/impl/toml_formatter.inl | 2 +- toml.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/toml++/impl/toml_formatter.inl b/include/toml++/impl/toml_formatter.inl index 201de9ce9e..8b4f633dca 100644 --- a/include/toml++/impl/toml_formatter.inl +++ b/include/toml++/impl/toml_formatter.inl @@ -91,7 +91,7 @@ TOML_ANON_NAMESPACE_START weight += 1u; val *= -1.0; } - return weight + static_cast(log10(val)) + 1u; + return weight + static_cast(abs(log10(val))) + 1u; } case node_type::boolean: return 5u; diff --git a/toml.hpp b/toml.hpp index ef3515b136..8fe1a0d5b8 100644 --- a/toml.hpp +++ b/toml.hpp @@ -17107,7 +17107,7 @@ TOML_ANON_NAMESPACE_START weight += 1u; val *= -1.0; } - return weight + static_cast(log10(val)) + 1u; + return weight + static_cast(abs(log10(val))) + 1u; } case node_type::boolean: return 5u; -- cgit 0.0.5-2-1-g0f52