Fixed false negative #4306: Detect loop access of empty STL container
This commit is contained in:
parent
5ddee75b5d
commit
865df4e207
|
@ -1551,10 +1551,14 @@ void CheckStl::readingEmptyStlContainer()
|
|||
if (i->type != Scope::eFunction)
|
||||
continue;
|
||||
|
||||
const Token* restartTok = nullptr;
|
||||
for (const Token *tok = i->classStart->next(); tok != i->classEnd; tok = tok->next()) {
|
||||
if (Token::Match(tok, "for|while|do|}")) { // Loops and end of scope clear the sets.
|
||||
if (Token::Match(tok, "for|while")) { // Loops and end of scope clear the sets.
|
||||
restartTok = tok->linkAt(1); // Check condition to catch looping over empty containers
|
||||
} else if (tok == restartTok || Token::Match(tok, "do|}")) {
|
||||
empty_map.clear();
|
||||
empty_nonmap.clear();
|
||||
restartTok = nullptr;
|
||||
}
|
||||
|
||||
if (!tok->varId())
|
||||
|
|
|
@ -2565,6 +2565,13 @@ private:
|
|||
" std::string strValue2 = CMap[1];\n"
|
||||
"}\n", true);
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Reading from empty STL container\n", errout.str());
|
||||
|
||||
// #4306
|
||||
check("void f(std::vector<int> v) {\n"
|
||||
" v.clear();\n"
|
||||
" for(int i = 0; i < v.size(); i++) { cout << v[i]; }\n"
|
||||
"}", true);
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Reading from empty STL container\n", errout.str());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue