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
10 changes: 10 additions & 0 deletions lib/calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,53 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr)
constexpr MathLib::bigint maxBitsSignedShift = maxBitsShift - 1;
switch (MathLib::encodeMultiChar(s)) {
case '+':
case '+=':
return wrap(x + y);
case '-':
case '-=':
return wrap(x - y);
case '*':
case '*=':
return wrap(x * y);
case '/':
case '/=':
if (isZero(y) || (std::is_signed<T>{} && y < 0)) {
if (error)
*error = true;
return R{};
}
return wrap(x / y);
case '%':
case '%=':
if (isZero(MathLib::bigint(y)) || (std::is_signed<T>{} && MathLib::bigint(y) < 0)) {
if (error)
*error = true;
return R{};
}
return wrap(MathLib::bigint(x) % MathLib::bigint(y));
case '&':
case '&=':
return wrap(MathLib::bigint(x) & MathLib::bigint(y));
case '|':
case '|=':
return wrap(MathLib::bigint(x) | MathLib::bigint(y));
case '^':
case '^=':
return wrap(MathLib::bigint(x) ^ MathLib::bigint(y));
case '>':
return wrap(x > y);
case '<':
return wrap(x < y);
case '<<':
case '<<=':
if (y >= maxBitsSignedShift || y < 0 || x < 0) {
if (error)
*error = true;
return R{};
}
return wrap(MathLib::bigint(x) << MathLib::bigint(y));
case '>>':
case '>>=':
if (y >= maxBitsSignedShift || y < 0 || x < 0) {
if (error)
*error = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ namespace ValueFlow
}

// Calculations..
else if ((parent->isArithmeticalOp() || parent->isComparisonOp() || (parent->tokType() == Token::eBitOp) ||
else if ((parent->isArithmeticalOp() || parent->isComparisonOp() || (parent->tokType() == Token::eBitOp) || (Token::Match(parent, "%assign%") && parent->str().size() > 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

For compound assignments, the computed result has to be converted to the type of the left operand. Today the value of c += 1 is computed like the value of c + 1, which is promoted to int, so it is never truncated.

int f(void) {
    unsigned char c = 255;
    int x = (c += 1);   // PR: `+=` always 256 (should be 0)
    if (x == 0)         // PR: new FP knownConditionTrueFalse "always false"
        return 1;
    return 0;
}
void g(int a[10]) { unsigned char c = 255; a[c += 1] = 0; } // PR: new FP arrayIndexOutOfBounds index 256

unsigned int u = 0; (u -= 1) likewise gets the value -1. Casting the result with castValue or setTokenValueCast, using the valueType() of parent->astOperand1(), would fix this. A test with an unsigned char or unsigned left operand would cover it.

(parent->tokType() == Token::eLogicalOp)) &&
parent->astOperand1() && parent->astOperand2()) {

Expand Down
6 changes: 6 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,12 @@ class TestValueFlow : public TestFixture {
"}\n";
ASSERT_NO_THROW(tokenValues(code, "<=>"));

code = "int f() {\n" // #15184
" int i = -1;\n"
" return 1 / (i += 1);\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, "+=", 0));

// Comparison of string
values = removeImpossible(tokenValues("f(\"xyz\" == \"xyz\");\n", "==")); // implementation defined
ASSERT_EQUALS(0U, values.size()); // <- no value
Expand Down
Loading