Skip to content
Merged
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: 4 additions & 6 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,16 @@ void execute(const Token *expr,
else {
bool error2 = false;
execute(expr->astOperand2(), programMemory, result, &error2);
if (error1 && error2)
if (error1 || error2)
*error = true;
if (error2)
*result = 1;
else
*result = !!*result;
}
}

else if (expr->str() == "||") {
execute(expr->astOperand1(), programMemory, result, error);
if (*result == 0 && *error == false)
if (*result == 1 && *error == false)
*result = 1;
else if (*result == 0 && *error == false)
execute(expr->astOperand2(), programMemory, result, error);
}

Expand Down
37 changes: 37 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class TestValueFlow : public TestFixture {
TEST_CASE(valueFlowCrashConstructorInitialization);

TEST_CASE(valueFlowUnknownMixedOperators);
TEST_CASE(valueFlowIdempotent);
}

static bool isNotTokValue(const ValueFlow::Value &val) {
Expand Down Expand Up @@ -5014,6 +5015,42 @@ class TestValueFlow : public TestFixture {

ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 1));
}

void valueFlowIdempotent() {
const char *code;

code = "void f(bool a, bool b) {\n"
" bool x = true;\n"
" if (a)\n"
" x = x && b;\n"
" bool result = x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 1));

code = "void f(bool a, bool b) {\n"
" bool x = false;\n"
" if (a)\n"
" x = x && b;\n"
" bool result = x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 0));

code = "void f(bool a, bool b) {\n"
" bool x = true;\n"
" if (a)\n"
" x = x || b;\n"
" bool result = x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 1));

code = "void f(bool a, bool b) {\n"
" bool x = false;\n"
" if (a)\n"
" x = x || b;\n"
" bool result = x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 0));
}
};

REGISTER_TEST(TestValueFlow)