From 5de683ec498e1eb846fe741cbc21d17aeecd086f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 2 Nov 2018 20:10:40 +0100 Subject: [PATCH] Container size: Fix false positive --- lib/valueflow.cpp | 9 +++++++++ test/testvalueflow.cpp | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index fb13b9bca..b1831b518 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3492,6 +3492,8 @@ static bool hasContainerSizeGuard(const Token *tok, unsigned int containerId) return false; } +static bool isContainerSizeChanged(unsigned int varId, const Token *start, const Token *end); + static bool isContainerSizeChangedByFunction(const Token *tok) { const Token *parent = tok->astParent(); @@ -3536,6 +3538,13 @@ static void valueFlowContainerForward(const Token *tok, unsigned int containerId while (nullptr != (tok = tok->next())) { if (Token::Match(tok, "[{}]")) 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) continue; if (Token::Match(tok, "%name% =")) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index ad63b1c3f..350cb66c4 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3422,6 +3422,14 @@ private: "}"; ASSERT(tokenValues(code, "s [").empty()); + // valueFlowContainerForward, loop + code = "void f() {\n" + " std::stack links;\n" + " while (!links.empty() || indentlevel)\n" + " links.push(tok);\n" + "}"; + ASSERT(tokenValues(code, "links . empty").empty()); + // valueFlowContainerForward, function call code = "void f() {\n" " std::list x;\n"