From 84dd0c961f517e606dddbcd12249f7ea488beb60 Mon Sep 17 00:00:00 2001 From: shaneasd Date: Fri, 4 Sep 2020 13:06:26 +0800 Subject: [PATCH] False positive dynamic_cast auto variable which has been checked against null (#2769) --- lib/valueflow.cpp | 2 +- test/testcondition.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 746ed1f35..bec2614aa 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -488,7 +488,7 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti // cast.. if (const Token *castType = getCastTypeStartToken(parent)) { - if (astIsPointer(tok) && value.valueType == ValueFlow::Value::INT && + if (((tok->valueType() == nullptr && value.isImpossible()) || astIsPointer(tok)) && value.valueType == ValueFlow::Value::INT && Token::simpleMatch(parent->astOperand1(), "dynamic_cast")) return; const ValueType &valueType = ValueType::parseDecl(castType, settings); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 2364be80f..188cc7cb0 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3306,6 +3306,17 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("int foo() {\n" + " auto x = getX();\n" + " if (x == nullptr)\n" + " return 1;\n" + " auto y = dynamic_cast(x)\n" + " if (y == nullptr)\n" + " return 2;\n" + " return 3;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // handleKnownValuesInLoop check("bool g();\n" "void f(bool x) {\n"