Fix 10642: False positive: Possible dereference of an invalid iterator: v.begin()+v.size()-1 (#3630)

This commit is contained in:
Paul Fultz II 2021-12-15 12:31:28 -06:00 committed by GitHub
parent d2dd4e54b9
commit 6681576707
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -7206,6 +7206,10 @@ static void valueFlowIteratorInfer(TokenList *tokenlist, const Settings *setting
values.remove_if([&](const ValueFlow::Value& v) { values.remove_if([&](const ValueFlow::Value& v) {
if (!v.isImpossible()) if (!v.isImpossible())
return true; return true;
if (!v.condition)
return true;
if (v.bound != ValueFlow::Value::Bound::Point)
return true;
if (v.isIteratorEndValue() && v.intvalue <= 0) if (v.isIteratorEndValue() && v.intvalue <= 0)
return true; return true;
if (v.isIteratorStartValue() && v.intvalue >= 0) if (v.isIteratorStartValue() && v.intvalue >= 0)

View File

@ -4200,6 +4200,12 @@ private:
ASSERT_EQUALS( ASSERT_EQUALS(
"[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());
// #10642
check("int f(std::vector<int> v) {\n"
" return *(v.begin() + v.size() - 1);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void dereferenceInvalidIterator2() { void dereferenceInvalidIterator2() {