Skip to content

Commit c9d2e55

Browse files
authored
Fix issue 10035: FP: knownConditionTrueFalse when bool updated in for loop (cppcheck-opensource#2953)
1 parent fd520b4 commit c9d2e55

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

lib/programmemory.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,18 +507,16 @@ void execute(const Token *expr,
507507
else {
508508
bool error2 = false;
509509
execute(expr->astOperand2(), programMemory, result, &error2);
510-
if (error1 && error2)
510+
if (error1 || error2)
511511
*error = true;
512-
if (error2)
513-
*result = 1;
514-
else
515-
*result = !!*result;
516512
}
517513
}
518514

519515
else if (expr->str() == "||") {
520516
execute(expr->astOperand1(), programMemory, result, error);
521-
if (*result == 0 && *error == false)
517+
if (*result == 1 && *error == false)
518+
*result = 1;
519+
else if (*result == 0 && *error == false)
522520
execute(expr->astOperand2(), programMemory, result, error);
523521
}
524522

test/testvalueflow.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class TestValueFlow : public TestFixture {
140140
TEST_CASE(valueFlowCrashConstructorInitialization);
141141

142142
TEST_CASE(valueFlowUnknownMixedOperators);
143+
TEST_CASE(valueFlowIdempotent);
143144
}
144145

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

50155016
ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 1));
50165017
}
5018+
5019+
void valueFlowIdempotent() {
5020+
const char *code;
5021+
5022+
code = "void f(bool a, bool b) {\n"
5023+
" bool x = true;\n"
5024+
" if (a)\n"
5025+
" x = x && b;\n"
5026+
" bool result = x;\n"
5027+
"}\n";
5028+
ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 1));
5029+
5030+
code = "void f(bool a, bool b) {\n"
5031+
" bool x = false;\n"
5032+
" if (a)\n"
5033+
" x = x && b;\n"
5034+
" bool result = x;\n"
5035+
"}\n";
5036+
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 0));
5037+
5038+
code = "void f(bool a, bool b) {\n"
5039+
" bool x = true;\n"
5040+
" if (a)\n"
5041+
" x = x || b;\n"
5042+
" bool result = x;\n"
5043+
"}\n";
5044+
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 1));
5045+
5046+
code = "void f(bool a, bool b) {\n"
5047+
" bool x = false;\n"
5048+
" if (a)\n"
5049+
" x = x || b;\n"
5050+
" bool result = x;\n"
5051+
"}\n";
5052+
ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 0));
5053+
}
50175054
};
50185055

50195056
REGISTER_TEST(TestValueFlow)

0 commit comments

Comments
 (0)