From 71c228a01a26c85ae26272e11b8a7b90a44b89d9 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 10 Aug 2020 22:07:22 -0500 Subject: [PATCH] Check for containers that modify the size using square bracket --- lib/valueflow.cpp | 2 +- test/testvalueflow.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d58d08689..32b6a0eb9 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2391,7 +2391,7 @@ struct ValueFlowForwardAnalyzer : ForwardAnalyzer { return Action::Invalid; if (match(tok)) { const Token* parent = tok->astParent(); - if ((Token::Match(parent, "*|[") || (parent && parent->originalName() == "->")) && getIndirect(tok) <= 0) + if (astIsPointer(tok) && (Token::Match(parent, "*|[") || (parent && parent->originalName() == "->")) && getIndirect(tok) <= 0) return Action::Read; // Action read = Action::Read; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 5220c9f2e..77f3f06b0 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -4471,6 +4471,14 @@ private: " ints.front();\n" "}"; ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front", ValueFlow::Value::CONTAINER_SIZE), 2)); + + code = "int f(bool b) {\n" + " std::map m;\n" + " if (b)\n" + " m[0] = 1;\n" + " return m.at(0);\n" + "}\n"; + ASSERT_EQUALS("", isPossibleContainerSizeValue(tokenValues(code, "m . at", ValueFlow::Value::CONTAINER_SIZE), 0)); } void valueFlowDynamicBufferSize() {