Skip to content

Commit 90f9bee

Browse files
committed
Fix #15288 (False positive: accessMoved in loop)
1 parent 6a4fedd commit 90f9bee

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎lib/vf_analyzers.cpp‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,19 @@ static bool stateCoversCondition(const Token* tok, const ProgramMemory& pm, int
6969
return true;
7070
if (tok->varId() > 0 || Token::simpleMatch(tok, ".")) {
7171
const ProgramMemory::Values* values = pm.getValues(tok->exprId());
72-
return values && !values->empty() && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
72+
if (!values || values->empty())
73+
return false;
74+
// The range of a bool (not <= -1, not >= 2) does not say anything about its value, use the
75+
// expression that the bool is known to be equal to instead
76+
if (astIsBool(tok) && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
77+
return v.isImpossible() && (v.intvalue < 0 || v.intvalue > 1);
78+
})) {
79+
return std::any_of(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) {
80+
return v.isSymbolicValue() && v.isKnown() && v.intvalue == 0 &&
81+
stateCoversCondition(v.tokvalue, pm, depth - 1);
82+
});
83+
}
84+
return std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
7385
return v.isIntValue();
7486
});
7587
}

‎test/testother.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ class TestOther : public TestFixture {
286286
TEST_CASE(doubleMoveMemberInitialization2);
287287
TEST_CASE(doubleMoveMemberInitialization3); // #9974
288288
TEST_CASE(doubleMoveMemberInitialization4);
289+
TEST_CASE(moveInLoopBoolCondition);
289290
TEST_CASE(moveAndAssign1);
290291
TEST_CASE(moveAndAssign2);
291292
TEST_CASE(moveAssignMoveAssign);
@@ -13040,6 +13041,31 @@ class TestOther : public TestFixture {
1304013041
ASSERT_EQUALS("[test.cpp:3:51]: (warning, inconclusive) Access of moved variable 'd'. [accessMoved]\n", errout_str());
1304113042
}
1304213043

13044+
void moveInLoopBoolCondition() { // #15288
13045+
check("struct W { std::vector<int> v; W flip() const; };\n"
13046+
"W w[2];\n"
13047+
"void f(W m) {\n"
13048+
" for (int i = 0; i < 2; i++) {\n"
13049+
" const bool first = (i == 0);\n"
13050+
" if (first) {\n"
13051+
" w[i] = m.flip();\n"
13052+
" } else {\n"
13053+
" w[i] = std::move(m);\n"
13054+
" }\n"
13055+
" }\n"
13056+
"}\n");
13057+
ASSERT_EQUALS("", errout_str());
13058+
13059+
check("struct W { std::vector<int> v; };\n"
13060+
"W w[2];\n"
13061+
"void f(W m) {\n"
13062+
" for (int i = 0; i < 2; i++) {\n"
13063+
" w[i] = std::move(m);\n"
13064+
" }\n"
13065+
"}\n");
13066+
ASSERT_EQUALS("[test.cpp:5:26]: (warning) Access of moved variable 'm'. [accessMoved]\n", errout_str());
13067+
}
13068+
1304313069
void moveAndAssign1() {
1304413070
check("A g(A a);\n"
1304513071
"void f() {\n"

0 commit comments

Comments
 (0)