From 96598dcbb8158b8ed8962432822fb41e5ed4e242 Mon Sep 17 00:00:00 2001 From: myrrc Date: Tue, 10 Nov 2020 19:48:32 +0300 Subject: [PATCH] fixed the UB loop --- base/common/wide_integer_impl.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/base/common/wide_integer_impl.h b/base/common/wide_integer_impl.h index 4b4d81ff4e3..f2d94bcd80c 100644 --- a/base/common/wide_integer_impl.h +++ b/base/common/wide_integer_impl.h @@ -261,15 +261,16 @@ struct integer::_impl const long double rhs_max_int_count = rhs_long_double / max_int; - // Won't fit only if long double can hold values >= 2^(64 * 3). - const uint64_t rhs_max_int_count_max_int_count = rhs_max_int_count / max_int; - + // We can't just get the number of iterations like rhs_max_int_count / max_int as it may not fit it int64_t. long double rhs_max_int_count_acc = rhs_max_int_count; self = 0; - for (uint64_t i = 0; i < rhs_max_int_count_max_int_count; ++i) + while (rhs_max_int_count_acc > max_int_long_double) + { self += max_int; + rhs_max_int_count_acc -= max_int_long_double; + } self *= max_int;