From 7c9baa119555be6b1db67e0f2d8c3d91df63b872 Mon Sep 17 00:00:00 2001 From: Zhaoqi Xu Date: Tue, 20 Oct 2026 06:50:37 +0800 Subject: [PATCH] Fix hang when a floating point value is too big for den_size Fixes #711 --- include/boost/ut.hpp | 8 +++++++- test/ut/ut.cpp | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/boost/ut.hpp b/include/boost/ut.hpp index d22ad812..302415f7 100644 --- a/include/boost/ut.hpp +++ b/include/boost/ut.hpp @@ -421,8 +421,14 @@ template TValue tmp{}; do { value *= 10; - tmp = value - T(value); ++result; + // Converting a value that does not fit in T is undefined (and in practice + // never yields a zero remainder, so the loop would not terminate). Values + // this large have no fractional digits left anyway. + if (not(value < TValue(std::numeric_limits::max() / 2 + 1))) { + break; + } + tmp = value - T(value); } while (tmp > precision); return result; diff --git a/test/ut/ut.cpp b/test/ut/ut.cpp index 45393eb3..ed336002 100644 --- a/test/ut/ut.cpp +++ b/test/ut/ut.cpp @@ -583,6 +583,10 @@ int main() { // NOLINT(readability-function-size) test_assert(12345678.9f == 12345678.9_f); test_assert(111111.42f == 111111.42_f); test_assert(1111111111.42 == 1111111111.42_d); + test_assert(_d(1.e19) == 1.e19); + test_assert(1.e19 == _d(1.e19)); + test_assert(_d(1.e300) == 1.e300); + test_assert(_f(1.e20f) == 1.e20f); test_assert(42_i == 42); test_assert(43 == 43_i); test_assert(not(1_i == 2));