Skip to content

Commit b685862

Browse files
committed
cppcheck-opensource#6779 internal error: division overflow. Previous error handling (see cppcheck-opensource#4520) was too restrictive.
1 parent 4172011 commit b685862

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

lib/mathlib.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,10 @@ std::string MathLib::divide(const std::string &first, const std::string &second)
617617
if (MathLib::isInt(first) && MathLib::isInt(second)) {
618618
const bigint a = toLongNumber(first);
619619
const bigint b = toLongNumber(second);
620-
if (a == std::numeric_limits<bigint>::min())
621-
throw InternalError(0, "Internal Error: Division overflow");
622620
if (b == 0)
623621
throw InternalError(0, "Internal Error: Division by zero");
622+
if (a == std::numeric_limits<bigint>::min() && std::abs(b)<=1)
623+
throw InternalError(0, "Internal Error: Division overflow");
624624
return toString(toLongNumber(first) / b) + intsuffix(first, second);
625625
} else if (isNullValue(second)) {
626626
if (isNullValue(first))

test/testmathlib.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class TestMathLib : public TestFixture {
139139
ASSERT_EQUALS("1" , MathLib::divide("3", "2"));
140140
ASSERT_THROW(MathLib::divide("123", "0"), InternalError); // throw
141141
ASSERT_THROW(MathLib::divide("-9223372036854775808", "-1"), InternalError); // #4520 - out of range => throw
142+
ASSERT_EQUALS("4611686018427387904", MathLib::divide("-9223372036854775808", "-2")); // #6679
142143
MathLib::divide("123", "0.0"); // don't throw
143144

144145
// Unknown action should throw exception

0 commit comments

Comments
 (0)