Fix 10657: False positive; ValueFlow - invalid iterator (#3636)

This commit is contained in:
Paul Fultz II 2021-12-16 15:34:51 -06:00 committed by GitHub
parent ac4f4258a0
commit 398fa28021
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -7188,7 +7188,7 @@ struct IteratorConditionHandler : SimpleConditionHandler {
if (!values.empty()) { if (!values.empty()) {
cond.vartok = tok->astOperand2(); cond.vartok = tok->astOperand2();
} else { } else {
values = getIteratorValues(tok->astOperand2()->values()); values = getIteratorValues(tok->astOperand2()->values(), &kind);
if (!values.empty()) if (!values.empty())
cond.vartok = tok->astOperand1(); cond.vartok = tok->astOperand1();
} }

View File

@ -4201,6 +4201,20 @@ private:
"[test.cpp:4] -> [test.cpp:4]: (warning) Either the condition 'i==v.end()' is redundant or there is possible dereference of an invalid iterator: i+1.\n", "[test.cpp:4] -> [test.cpp:4]: (warning) Either the condition 'i==v.end()' is redundant or there is possible dereference of an invalid iterator: i+1.\n",
errout.str()); errout.str());
// #10657
check("std::list<int> mValues;\n"
"typedef std::list<int>::iterator ValueIterator;\n"
"void foo(ValueIterator beginValue, ValueIterator endValue) {\n"
" ValueIterator prevValue = beginValue;\n"
" ValueIterator curValue = beginValue;\n"
" for (++curValue; prevValue != endValue && curValue != mValues.end(); ++curValue) {\n"
" a = bar(*curValue);\n"
" prevValue = curValue;\n"
" }\n"
" if (endValue == mValues.end()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #10642 // #10642
check("int f(std::vector<int> v) {\n" check("int f(std::vector<int> v) {\n"
" return *(v.begin() + v.size() - 1);\n" " return *(v.begin() + v.size() - 1);\n"