Skip to content

Commit 53b0c3d

Browse files
committed
Fix potential overflow in comparison
1 parent 2ad7574 commit 53b0c3d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

include/boost/decimal/detail/comparison.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,25 @@ BOOST_DECIMAL_FORCE_INLINE constexpr auto equality_impl(DecimalType lhs, Decimal
6868
}
6969

7070
// Step 5: Normalize the significand and compare
71-
delta_exp >= 0 ? lhs_sig *= detail::pow10(static_cast<comp_type>(delta_exp)) :
72-
rhs_sig *= detail::pow10(static_cast<comp_type>(-delta_exp));
71+
// Instead of multiplying the larger number, divide the smaller one
72+
if (delta_exp >= 0)
73+
{
74+
// Check if we can divide rhs_sig safely
75+
if (delta_exp > 0 && rhs_sig % detail::pow10(static_cast<comp_type>(delta_exp)) != 0)
76+
{
77+
return false;
78+
}
79+
rhs_sig /= detail::pow10(static_cast<comp_type>(delta_exp));
80+
}
81+
else
82+
{
83+
// Check if we can divide lhs_sig safely
84+
if (lhs_sig % detail::pow10(static_cast<comp_type>(-delta_exp)) != 0)
85+
{
86+
return false;
87+
}
88+
lhs_sig /= detail::pow10(static_cast<comp_type>(-delta_exp));
89+
}
7390

7491
return lhs_sig == rhs_sig;
7592
}

0 commit comments

Comments
 (0)