Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid overflow further in rational::operator+= #50

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion include/boost/rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ inline rational<IntType> operator- (const rational<IntType>& r)
template <typename IntType>
BOOST_CXX14_CONSTEXPR rational<IntType>& rational<IntType>::operator+= (const rational<IntType>& r)
{
// Prevent division by zero below if *this == 0, r == 0 and num_g == 0.
if (r.num == IntType(0))
return *this; // r == 0 => *this need not be changed.

// This calculation avoids overflow, and minimises the number of expensive
// calculations. Thanks to Nickolay Mladenov for this algorithm.
//
Expand All @@ -534,15 +538,22 @@ BOOST_CXX14_CONSTEXPR rational<IntType>& rational<IntType>::operator+= (const ra
// Which proves that instead of normalizing the result, it is better to
// divide num and den by gcd((a*d1 + c*b1), g)

// First dividing, then multiplying by num_g avoids overflow even further,
// at the cost of a branch, an extra GCD calculation, two integer divisions
// and one integer multiplication.

// Protect against self-modification
IntType r_num = r.num;
IntType r_den = r.den;

const IntType num_g = integer::gcd(num, r_num);
IntType g = integer::gcd(den, r_den);
den /= g; // = b1 from the calculations above
num = num * (r_den / g) + r_num * den;
num = (num / num_g) * (r_den / g) + (r_num / num_g) * den;

g = integer::gcd(num, g);
num /= g;
num *= num_g;
den *= r_den/g;

return *this;
Expand Down
33 changes: 33 additions & 0 deletions test/rational_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,39 @@ BOOST_AUTO_TEST_SUITE_END()
// The bugs, patches, and requests checking suite
BOOST_AUTO_TEST_SUITE( bug_patch_request_suite )

BOOST_AUTO_TEST_CASE( operator_plus_overflow_test )
{
MyOverflowingUnsigned const base = 10;
MyOverflowingUnsigned num1 = 1;
while ( num1 < UINT_MAX / base ) num1 *= base;
if ( UINT_MAX / num1 < 4 ) // e.g. 64-bit unsigned
{
num1 /= base;
num1 *= 7;
}
else // e.g. 32-bit unsigned
{
num1 *= 2;
}
MyOverflowingUnsigned const num2 = num1 * 2;

// Other unsigned types are not covered by this test
// (but it should still pass on all platforms).
if ( sizeof( MyOverflowingUnsigned ) == 4
|| sizeof( MyOverflowingUnsigned ) == 8 )
{
BOOST_TEST( UINT_MAX - num1 < num2 );
}

MyOverflowingUnsigned const denom = 3;
boost::rational<MyOverflowingUnsigned> const r1( num1, denom ),
r2( num2, denom );
// This test succeeds when MyOverflowingUnsigned is replaced with a built-in
// unsigned type due to unsigned integer overflow wraparound. But it guards
// against the undefined behavior of signed integer overflow.
BOOST_TEST( r1 + r2 == num1 );
}

// "rational operator< can overflow"
BOOST_AUTO_TEST_CASE( bug_798357_test )
{
Expand Down