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
60 changes: 42 additions & 18 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5180,8 +5180,21 @@ static bool valueFlowForLoop2(const Token *tok,
if (error)
return false;
execute(secondExpression, programMemory, &result, &error, settings);
if (result == 0) // 2nd expression is false => no looping
if (result == 0) {
if (!error) { // 2nd expression is definitely false => no looping
ProgramMemory startMemory(programMemory);
ProgramMemory endMemory(programMemory);

if (memory1)
memory1->swap(startMemory);
if (memory2)
memory2->swap(endMemory);
if (memoryAfter)
memoryAfter->swap(programMemory);
return true;
}
return false;
}
if (error) {
// If a variable is reassigned in second expression, return false
bool reassign = false;
Expand Down Expand Up @@ -5390,23 +5403,34 @@ static void valueFlowForLoop(const TokenList &tokenlist, const SymbolDatabase& s
} else {
ProgramMemory mem1, mem2, memAfter;
if (valueFlowForLoop2(tok, &mem1, &mem2, &memAfter, settings)) {
for (const auto& p : mem1) {
if (!p.second.isIntValue())
continue;
if (p.second.isImpossible())
continue;
if (p.first.tok->varId() == 0)
continue;
valueFlowForLoopSimplify(bodyStart, p.first.tok, false, p.second.intvalue, tokenlist, errorLogger, settings);
}
for (const auto& p : mem2) {
if (!p.second.isIntValue())
continue;
if (p.second.isImpossible())
continue;
if (p.first.tok->varId() == 0)
continue;
valueFlowForLoopSimplify(bodyStart, p.first.tok, false, p.second.intvalue, tokenlist, errorLogger, settings);
if (mem1 == memAfter) { // #8192 check if loop never runs
Token* condTok = getCondTok(tok);
if (condTok && !condTok->hasKnownIntValue()) {
ValueFlow::Value v(0);
v.setKnown();
ValueFlow::setTokenValue(condTok, std::move(v), settings);
}
} else {
for (const auto& p : mem1) {
if (!p.second.isIntValue())
continue;
if (p.second.isImpossible())
continue;
if (p.first.tok->varId() == 0)
continue;

valueFlowForLoopSimplify(bodyStart, p.first.tok, false, p.second.intvalue, tokenlist, errorLogger, settings);
}
for (const auto& p : mem2) {
if (!p.second.isIntValue())
continue;
if (p.second.isImpossible())
continue;
if (p.first.tok->varId() == 0)
continue;

valueFlowForLoopSimplify(bodyStart, p.first.tok, false, p.second.intvalue, tokenlist, errorLogger, settings);
}
}
for (const auto& p : memAfter) {
if (!p.second.isIntValue())
Expand Down
2 changes: 1 addition & 1 deletion test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5581,7 +5581,7 @@ class TestCondition : public TestFixture {
check("void f() {\n" // #8192
" for (int i = 0; i > 10; ++i) {}\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Condition 'i>10' is always false\n", "", errout_str());
ASSERT_EQUALS("[test.cpp:2:23]: (style) Condition 'i>10' is always false [knownConditionTrueFalse]\n", errout_str());

check("void f() {\n"
" for (int i = 1000; i < 20; ++i) {}\n"
Expand Down
Loading