diff --git a/Makefile b/Makefile index 44cef58bf04..b5d2dc6ce33 100644 --- a/Makefile +++ b/Makefile @@ -628,7 +628,7 @@ $(libcppdir)/keywords.o: lib/keywords.cpp lib/config.h lib/keywords.h lib/standa $(libcppdir)/library.o: lib/library.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/refthunk.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/library.cpp -$(libcppdir)/mathlib.o: lib/mathlib.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errortypes.h lib/mathlib.h lib/smallvector.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h +$(libcppdir)/mathlib.o: lib/mathlib.cpp externals/simplecpp/simplecpp.h lib/calculate.h lib/config.h lib/errortypes.h lib/mathlib.h lib/smallvector.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/mathlib.cpp $(libcppdir)/path.o: lib/path.cpp externals/simplecpp/simplecpp.h lib/config.h lib/path.h lib/standards.h lib/utils.h diff --git a/lib/calculate.h b/lib/calculate.h index 470000f95ad..6d4f2f1f861 100644 --- a/lib/calculate.h +++ b/lib/calculate.h @@ -23,6 +23,7 @@ #include "errortypes.h" #include #include +#include template bool isEqual(T x, T y) @@ -46,6 +47,39 @@ bool isZero(T x) return isEqual(x, T(0)); } +/** + * x + y, x - y or x * y. Signed integer overflow is undefined behaviour, so for a signed integer + * type the operation is done on the unsigned type and the result wraps. + */ +template +T calculateArithmetic(char op, T x, T y, std::true_type /*isSignedInteger*/) +{ + using U = typename std::make_unsigned::type; + const U ux = static_cast(x); + const U uy = static_cast(y); + if (op == '+') + return static_cast(ux + uy); + if (op == '-') + return static_cast(ux - uy); + return static_cast(ux * uy); +} + +template +T calculateArithmetic(char op, T x, T y, std::false_type /*isSignedInteger*/) +{ + if (op == '+') + return x + y; + if (op == '-') + return x - y; + return x * y; +} + +template +T calculateArithmetic(char op, T x, T y) +{ + return calculateArithmetic(op, x, y, std::integral_constant::value && std::is_signed::value>{}); +} + /** * @throws InternalError thrown in case of unknown operator */ @@ -61,13 +95,13 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr) switch (MathLib::encodeMultiChar(s)) { case '+': case '+=': - return wrap(x + y); + return wrap(calculateArithmetic('+', x, y)); case '-': case '-=': - return wrap(x - y); + return wrap(calculateArithmetic('-', x, y)); case '*': case '*=': - return wrap(x * y); + return wrap(calculateArithmetic('*', x, y)); case '/': case '/=': if (isZero(y) || (std::is_signed{} && y < 0)) { @@ -126,7 +160,7 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr) case '<=': return wrap(x <= y); case '<=>': - return wrap(x - y); + return wrap(calculateArithmetic('-', x, y)); } throw InternalError(nullptr, "Unknown operator: " + s); } diff --git a/lib/infer.cpp b/lib/infer.cpp index cad0761f68b..44af34621cf 100644 --- a/lib/infer.cpp +++ b/lib/infer.cpp @@ -175,8 +175,12 @@ namespace { friend Interval operator-(const Interval& lhs, const Interval& rhs) { Interval result; - result.minvalue = Interval::apply(lhs.minvalue, rhs.maxvalue, std::minus{}); - result.maxvalue = Interval::apply(lhs.maxvalue, rhs.minvalue, std::minus{}); + result.minvalue = Interval::apply(lhs.minvalue, rhs.maxvalue, [](MathLib::bigint x, MathLib::bigint y) { + return calculateArithmetic('-', x, y); + }); + result.maxvalue = Interval::apply(lhs.maxvalue, rhs.minvalue, [](MathLib::bigint x, MathLib::bigint y) { + return calculateArithmetic('-', x, y); + }); if (!result.minvalue.empty()) result.minRef = merge(lhs.minRef, rhs.maxRef); if (!result.maxvalue.empty()) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 9aba7118858..77bec7eb465 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -18,6 +18,7 @@ #include "mathlib.h" +#include "calculate.h" #include "errortypes.h" #include "token.h" #include "utils.h" @@ -186,13 +187,9 @@ MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const Mat } else { switch (op) { case '+': - temp.mIntValue += v2.mIntValue; - break; case '-': - temp.mIntValue -= v2.mIntValue; - break; case '*': - temp.mIntValue *= v2.mIntValue; + temp.mIntValue = calculateArithmetic(op, temp.mIntValue, v2.mIntValue); break; case '/': if (v2.mIntValue == 0) @@ -204,7 +201,11 @@ MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const Mat case '%': if (v2.mIntValue == 0) throw InternalError(nullptr, "Internal Error: Division by zero"); - temp.mIntValue %= v2.mIntValue; + // x % -1 is 0, but the division overflows for the minimum value + if (v2.mIntValue == -1) + temp.mIntValue = 0; + else + temp.mIntValue %= v2.mIntValue; break; case '&': temp.mIntValue &= v2.mIntValue; @@ -1119,7 +1120,7 @@ std::string MathLib::add(const std::string & first, const std::string & second) return (value(first) + value(second)).str(); #else if (MathLib::isInt(first) && MathLib::isInt(second)) { - return MathLib::toString(toBigNumber(first) + toBigNumber(second)) + intsuffix(first, second); + return MathLib::toString(calculateArithmetic('+', toBigNumber(first), toBigNumber(second))) + intsuffix(first, second); } double d1 = toDoubleNumber(first); @@ -1141,7 +1142,7 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon return (value(first) - value(second)).str(); #else if (MathLib::isInt(first) && MathLib::isInt(second)) { - return MathLib::toString(toBigNumber(first) - toBigNumber(second)) + intsuffix(first, second); + return MathLib::toString(calculateArithmetic('-', toBigNumber(first), toBigNumber(second))) + intsuffix(first, second); } if (first == second) @@ -1189,7 +1190,7 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon return (value(first) * value(second)).str(); #else if (MathLib::isInt(first) && MathLib::isInt(second)) { - return MathLib::toString(toBigNumber(first) * toBigNumber(second)) + intsuffix(first, second); + return MathLib::toString(calculateArithmetic('*', toBigNumber(first), toBigNumber(second))) + intsuffix(first, second); } return toString(toDoubleNumber(first) * toDoubleNumber(second)); #endif @@ -1204,6 +1205,9 @@ std::string MathLib::mod(const std::string &first, const std::string &second) const bigint b = toBigNumber(second); if (b == 0) throw InternalError(nullptr, "Internal Error: Division by zero"); + // x % -1 is 0, but the division overflows for the minimum value + if (b == -1) + return "0" + intsuffix(first, second); return MathLib::toString(toBigNumber(first) % b) + intsuffix(first, second); } return toString(std::fmod(toDoubleNumber(first),toDoubleNumber(second))); diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 95d977e5f26..3672e7f75c6 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -2104,7 +2104,7 @@ namespace { } if (expr->str() == "-") { for (ValueFlow::Value& v : lhs) { - v.intvalue = -v.intvalue; + v.intvalue = calculateArithmetic('-', 0, v.intvalue); v.invertBound(); } } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 7f4cd5584e8..40ca99d1f8d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1146,12 +1146,13 @@ static void valueFlowEnumValue(SymbolDatabase & symboldatabase, const Settings & if (rhs && rhs->hasKnownIntValue()) { enumerator.value = rhs->getKnownIntValue(); enumerator.value_known = true; - value = enumerator.value + 1; + value = calculateArithmetic('+', enumerator.value, 1); prev_enum_is_known = true; } else prev_enum_is_known = false; } else if (prev_enum_is_known) { - enumerator.value = value++; + enumerator.value = value; + value = calculateArithmetic('+', value, 1); enumerator.value_known = true; } } @@ -6283,16 +6284,16 @@ const Token* ValueFlow::solveExprValue(const Token* expr, if (binaryTok && expr->str().size() == 1) { switch (expr->str()[0]) { case '+': { - value.intvalue -= intval; + value.intvalue = calculateArithmetic('-', value.intvalue, intval); return ValueFlow::solveExprValue(binaryTok, eval, value); } case '-': { if (rhs) { - value.intvalue = intval - value.intvalue; + value.intvalue = calculateArithmetic('-', intval, value.intvalue); // c - x >= a <=> x <= c - a value.invertBound(); } else - value.intvalue += intval; + value.intvalue = calculateArithmetic('+', value.intvalue, intval); return ValueFlow::solveExprValue(binaryTok, eval, value); } case '*': { diff --git a/lib/vf_analyzers.cpp b/lib/vf_analyzers.cpp index ccadc3ef623..7c9c8d12e85 100644 --- a/lib/vf_analyzers.cpp +++ b/lib/vf_analyzers.cpp @@ -371,7 +371,7 @@ struct ValueFlowAnalyzer : Analyzer { const std::string opName(inc ? "incremented" : "decremented"); if (d == Direction::Reverse) inc = !inc; - value->intvalue += (inc ? 1 : -1); + value->intvalue = calculateArithmetic(inc ? '+' : '-', value->intvalue, 1); /* Truncate value */ const ValueType *dst = tok->valueType(); diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index 2e5b229c0a7..c658b4bf96d 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -682,16 +682,16 @@ namespace ValueFlow const ValueType *dst = tok->valueType(); if (dst) { const size_t sz = dst->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer); - MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue + 1, sz, dst, settings.platform); + MathLib::bigint newvalue = ValueFlow::truncateIntValue(calculateArithmetic('+', v.intvalue, 1), sz, dst, settings.platform); if (v.bound != ValueFlow::Value::Bound::Point) { if (newvalue < v.intvalue) { v.invertBound(); - newvalue -= 2; + newvalue = calculateArithmetic('-', newvalue, 2); } } v.intvalue = newvalue; } else { - v.intvalue = v.intvalue + 1; + v.intvalue = calculateArithmetic('+', v.intvalue, 1); } } else @@ -712,16 +712,16 @@ namespace ValueFlow const ValueType *dst = tok->valueType(); if (dst) { const size_t sz = dst->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer); - MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue - 1, sz, dst, settings.platform); + MathLib::bigint newvalue = ValueFlow::truncateIntValue(calculateArithmetic('-', v.intvalue, 1), sz, dst, settings.platform); if (v.bound != ValueFlow::Value::Bound::Point) { if (newvalue > v.intvalue) { v.invertBound(); - newvalue += 2; + newvalue = calculateArithmetic('+', newvalue, 2); } } v.intvalue = newvalue; } else { - v.intvalue = v.intvalue - 1; + v.intvalue = calculateArithmetic('-', v.intvalue, 1); } } else diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index a71b026618b..a61752ae290 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -297,7 +297,7 @@ $(libcppdir)/keywords.o: ../lib/keywords.cpp ../lib/config.h ../lib/keywords.h . $(libcppdir)/library.o: ../lib/library.cpp ../externals/tinyxml2/tinyxml2.h ../lib/astutils.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/refthunk.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/library.cpp -$(libcppdir)/mathlib.o: ../lib/mathlib.cpp ../externals/simplecpp/simplecpp.h ../lib/config.h ../lib/errortypes.h ../lib/mathlib.h ../lib/smallvector.h ../lib/templatesimplifier.h ../lib/token.h ../lib/utils.h ../lib/vfvalue.h +$(libcppdir)/mathlib.o: ../lib/mathlib.cpp ../externals/simplecpp/simplecpp.h ../lib/calculate.h ../lib/config.h ../lib/errortypes.h ../lib/mathlib.h ../lib/smallvector.h ../lib/templatesimplifier.h ../lib/token.h ../lib/utils.h ../lib/vfvalue.h $(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/mathlib.cpp $(libcppdir)/path.o: ../lib/path.cpp ../externals/simplecpp/simplecpp.h ../lib/config.h ../lib/path.h ../lib/standards.h ../lib/utils.h diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 800334926e5..77eb66d7b94 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -157,6 +157,13 @@ class TestMathLib : public TestFixture { ASSERT_THROW_INTERNAL_EQUALS(MathLib::divide("-9223372036854775808", "-1"), INTERNAL, "Internal Error: Division overflow"); // #4520 - out of range => throw ASSERT_EQUALS("4611686018427387904", MathLib::divide("-9223372036854775808", "-2")); // #6679 + // out of range results wrap without signed integer overflow + ASSERT_EQUALS("-9223372036854775808", MathLib::add("9223372036854775807", "1")); + ASSERT_EQUALS("9223372036854775807", MathLib::subtract("-9223372036854775808", "1")); + ASSERT_EQUALS("-2", MathLib::multiply("9223372036854775807", "2")); + ASSERT_EQUALS("0", MathLib::mod("-9223372036854775808", "-1")); + ASSERT_EQUALS("0", MathLib::calculate("-9223372036854775808", "-1", '%')); + // invoke for each supported action ASSERT_EQUALS("3", MathLib::calculate("2", "1", '+')); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 85ee8430a77..35e7656df3c 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -28,6 +28,7 @@ #include "tokenlist.h" #include +#include #include #include #include @@ -470,6 +471,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(enum18); TEST_CASE(enum19); TEST_CASE(enum20); // #14419 + TEST_CASE(enum21); TEST_CASE(struct1); @@ -7113,6 +7115,19 @@ class TestSymbolDatabase : public TestFixture { } } + void enum21() { // implicit values past the maximum wrap without signed integer overflow + GET_SYMBOL_DB("enum E { A = 0x7FFFFFFFFFFFFFFE, B, C };\n"); + ASSERT(db != nullptr); + const Scope* scope = db->findScopeByName("E"); + ASSERT(scope); + const Enumerator* B = scope->findEnumerator("B"); + ASSERT(B && B->value_known); + ASSERT_EQUALS(0x7FFFFFFFFFFFFFFELL + 1, B->value); + const Enumerator* C = scope->findEnumerator("C"); + ASSERT(C && C->value_known); + ASSERT_EQUALS(LLONG_MIN, C->value); + } + void struct1() { GET_SYMBOL_DB_C("struct deer {\n" " uint16_t a;\n" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index c7f0cd63eb0..95192d69a85 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1148,6 +1148,10 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(10, valueOfTok("x = static_cast(10);\n", "( 10 )").intvalue); ASSERT_EQUALS(0, valueOfTok("x = sizeof (struct {int a;}) * 0;\n", "*").intvalue); + // out of range results wrap without signed integer overflow + ASSERT_EQUALS(LLONG_MIN, valueOfTok("x = 0x7fffffffffffffff + 1;\n", "+").intvalue); + ASSERT_EQUALS(-2, valueOfTok("x = 0x7fffffffffffffff * 2;\n", "*").intvalue); + // Don't calculate or crash if there is UB or invalid operations ASSERT(tokenValues(";-1<<10;\n","<<").empty()); ASSERT(tokenValues(";10<<-1;\n","<<").empty()); @@ -2467,6 +2471,47 @@ class TestValueFlow : public TestFixture { "}\n"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); + // increment past the maximum wraps without signed integer overflow + code = "void f() {\n" + " long long x = 0x7fffffffffffffff;\n" + " x++;\n" + " a = x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 4U, LLONG_MIN)); + + // negating the minimum wraps without signed integer overflow + code = "long long f() {\n" + " long long m = -0x7fffffffffffffff - 1;\n" + " long long x = -m;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 4U, LLONG_MIN)); + + // prefix increment/decrement in an expression wraps without signed integer overflow + code = "long long f() {\n" + " long long m = 0x7fffffffffffffff;\n" + " long long x = ++m;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 4U, LLONG_MIN)); + + code = "long long f() {\n" + " long long m = -0x7fffffffffffffff - 1;\n" + " long long x = --m;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 4U, LLONG_MAX)); + + // solving chained subtractions of large constants wraps without signed integer overflow + code = "void f(long long a) {\n" + " long long y = a - 0x7fffffffffffffff;\n" + " long long x = y - 0x7fffffffffffffff;\n" + " long long w = x - 0x7fffffffffffffff;\n" + " if (w == 0) {}\n" + " if (a == 0) {}\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 4U, 2)); + code = "void f() {\n" " int x = 123;\n" " a = sizeof(x);\n"