Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 38 additions & 4 deletions lib/calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "errortypes.h"
#include <limits>
#include <string>
#include <type_traits>

template<class T>
bool isEqual(T x, T y)
Expand All @@ -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<class T>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Another signed overflow is left that the PR does not cover: solveExprValue in lib/valueflow.cpp:6287 (value.intvalue -= intval;) and :6296 (value.intvalue += intval;).

void f(long long x) {
    long long y = x + 0x7fffffffffffffff;
    long long z = y + 0x7fffffffffffffff;
    if (z == 3) {}
}

Under UBSan this gives:

lib/valueflow.cpp:6287:28: runtime error: signed integer overflow: -9223372036854775804 - 9223372036854775807 cannot be represented in type 'long long int'

The same chain with three - 0x7fffffffffffffff steps hits line 6296. If the goal is a testrunner that is clean with the sanitizer turned on, these sites need the helper as well.

T calculateArithmetic(char op, T x, T y, std::true_type /*isSignedInteger*/)
{
using U = typename std::make_unsigned<T>::type;
const U ux = static_cast<U>(x);
const U uy = static_cast<U>(y);
if (op == '+')
return static_cast<T>(ux + uy);
if (op == '-')
return static_cast<T>(ux - uy);
return static_cast<T>(ux * uy);
}

template<class T>
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<class T>
T calculateArithmetic(char op, T x, T y)
{
return calculateArithmetic(op, x, y, std::integral_constant<bool, std::is_integral<T>::value && std::is_signed<T>::value>{});
}

/**
* @throws InternalError thrown in case of unknown operator
*/
Expand All @@ -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<T>{} && y < 0)) {
Expand Down Expand Up @@ -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);
}
Expand Down
22 changes: 13 additions & 9 deletions lib/mathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include "mathlib.h"
#include "calculate.h"
#include "errortypes.h"
#include "token.h"
#include "utils.h"
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)));
Expand Down
2 changes: 1 addition & 1 deletion lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ namespace {
}
if (expr->str() == "-") {
for (ValueFlow::Value& v : lhs) {
v.intvalue = -v.intvalue;
v.intvalue = calculateArithmetic<MathLib::bigint>('-', 0, v.intvalue);
v.invertBound();
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MathLib::bigint>('+', 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<MathLib::bigint>('+', value, 1);
enumerator.value_known = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/vf_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MathLib::bigint>(inc ? '+' : '-', value->intvalue, 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This fixes the x++; statement path, but prefix ++x/--x used inside an expression goes through setTokenValue in lib/vf_settokenvalue.cpp, and that still overflows. Lines 685/694 do v.intvalue + 1 and lines 715/724 do v.intvalue - 1.

void f() {
    long long x = 0x7fffffffffffffff;
    long long y = ++x;
}

Under -fsanitize=signed-integer-overflow this gives:

lib/vf_settokenvalue.cpp:685:83: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long long int'

--x with x == LLONG_MIN gives the same error at line 715. Those sites could use calculateArithmetic too.


/* Truncate value */
const ValueType *dst = tok->valueType();
Expand Down
2 changes: 1 addition & 1 deletion oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/testmathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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", '+'));
Expand Down
15 changes: 15 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "tokenlist.h"

#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstring>
#include <iterator>
Expand Down Expand Up @@ -470,6 +471,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(enum18);
TEST_CASE(enum19);
TEST_CASE(enum20); // #14419
TEST_CASE(enum21);

TEST_CASE(struct1);

Expand Down Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,10 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(10, valueOfTok("x = static_cast<int>(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());
Expand Down Expand Up @@ -2467,6 +2471,22 @@ 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));

code = "void f() {\n"
" int x = 123;\n"
" a = sizeof(x);\n"
Expand Down
Loading