containerAccessOutOfBounds: Fix FPs for maps etc

This commit is contained in:
Daniel Marjamäki 2018-08-11 18:57:21 +02:00
parent c4000b27f8
commit 0e30bdef9d
2 changed files with 16 additions and 0 deletions

View File

@ -72,6 +72,8 @@ void CheckStl::outOfBounds()
outOfBoundsError(tok, &value, nullptr);
continue;
}
if (!container->arrayLike_indexOp && !container->stdStringLike)
continue;
if (value.intvalue == 0 && Token::Match(tok, "%name% [")) {
outOfBoundsError(tok, &value, nullptr);
continue;

View File

@ -216,6 +216,20 @@ private:
" v[i] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(std::map<int,int> x) {\n"
" if (x.empty()) { x[1] = 2; }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(std::string s) {\n"
" if (s.size() == 1) {\n"
" s[2] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("test.cpp:3:warning:Possible access out of bounds of container 's'; size=1, index=2\n"
"test.cpp:2:note:condition 's.size()==1'\n"
"test.cpp:3:note:Access out of bounds\n", errout.str());
}
void iterator1() {