From 37ef29889bd749fdc1f140659cc32a38d07dd161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 24 Aug 2021 16:06:51 +0200 Subject: [PATCH] Fix 10424: ExpressionAnalyzer assertion failure in librevisa --- lib/valueflow.cpp | 8 +++++++- test/testvalueflow.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 28cee8873..cc093d5b2 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7079,7 +7079,9 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold setTokenValue(const_cast(tok), value, settings); } else if (Token::Match(tok, "%name%|;|{|} %var% = %str% ;")) { const Token *containerTok = tok->next(); - if (containerTok && containerTok->valueType() && containerTok->valueType()->container && containerTok->valueType()->container->stdStringLike) { + if (containerTok->exprId() == 0) + continue; + if (containerTok->valueType() && containerTok->valueType()->container && containerTok->valueType()->container->stdStringLike) { ValueFlow::Value value(Token::getStrLength(containerTok->tokAt(2))); value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; value.setKnown(); @@ -7087,6 +7089,8 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold } } else if (Token::Match(tok, "%name%|;|{|}|> %var% = {") && Token::simpleMatch(tok->linkAt(3), "} ;")) { const Token* containerTok = tok->next(); + if (containerTok->exprId() == 0) + continue; if (astIsContainer(containerTok) && containerTok->valueType()->container->size_templateArgNo < 0) { std::vector values = getInitListSize(tok->tokAt(3), containerTok->valueType()->container); for (const ValueFlow::Value& value : values) @@ -7094,6 +7098,8 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold } } else if (Token::Match(tok, ". %name% (") && tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->container) { const Token* containerTok = tok->astOperand1(); + if (containerTok->exprId() == 0) + continue; Library::Container::Action action = containerTok->valueType()->container->getAction(tok->strAt(1)); if (action == Library::Container::Action::CLEAR) { ValueFlow::Value value(0); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 546d0e872..2348d7ecd 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -5669,6 +5669,16 @@ private: " return o;\n" "}}\n"; valueOfTok(code, "return"); + + code = "class dummy_resource : public instrument_resource {\n" + "public:\n" + " int reads;\n" + " static std::list log;\n" + "};\n" + "void dummy_reader_reset() {\n" + " dummy_resource::log.clear();\n" + "}\n"; + valueOfTok(code, "log"); } void valueFlowCrash() {