Container size: Fix false positive

This commit is contained in:
Daniel Marjamäki 2018-11-02 20:10:40 +01:00
parent e53be26f48
commit 5de683ec49
2 changed files with 17 additions and 0 deletions

View File

@ -3492,6 +3492,8 @@ static bool hasContainerSizeGuard(const Token *tok, unsigned int containerId)
return false; return false;
} }
static bool isContainerSizeChanged(unsigned int varId, const Token *start, const Token *end);
static bool isContainerSizeChangedByFunction(const Token *tok) static bool isContainerSizeChangedByFunction(const Token *tok)
{ {
const Token *parent = tok->astParent(); const Token *parent = tok->astParent();
@ -3536,6 +3538,13 @@ static void valueFlowContainerForward(const Token *tok, unsigned int containerId
while (nullptr != (tok = tok->next())) { while (nullptr != (tok = tok->next())) {
if (Token::Match(tok, "[{}]")) if (Token::Match(tok, "[{}]"))
break; break;
if (Token::Match(tok, "while|for (")) {
const Token *start = tok->linkAt(1)->next();
if (!Token::simpleMatch(start->link(), "{"))
break;
if (isContainerSizeChanged(containerId, start, start->link()))
break;
}
if (tok->varId() != containerId) if (tok->varId() != containerId)
continue; continue;
if (Token::Match(tok, "%name% =")) if (Token::Match(tok, "%name% ="))

View File

@ -3422,6 +3422,14 @@ private:
"}"; "}";
ASSERT(tokenValues(code, "s [").empty()); ASSERT(tokenValues(code, "s [").empty());
// valueFlowContainerForward, loop
code = "void f() {\n"
" std::stack<Token *> links;\n"
" while (!links.empty() || indentlevel)\n"
" links.push(tok);\n"
"}";
ASSERT(tokenValues(code, "links . empty").empty());
// valueFlowContainerForward, function call // valueFlowContainerForward, function call
code = "void f() {\n" code = "void f() {\n"
" std::list<int> x;\n" " std::list<int> x;\n"