From d838dc2129606b4d0276cf7f7d9d4d5293c74e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 4 Sep 2017 15:58:22 +0200 Subject: [PATCH] CheckCondition::multiCondition2: Fix FN for pointers --- lib/checkcondition.cpp | 11 +++++++---- test/testcondition.cpp | 8 ++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index b1acf4305..9ef95295e 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -494,9 +494,13 @@ void CheckCondition::multiCondition2() if (cond->varId()) { vars.insert(cond->varId()); const Variable *var = cond->variable(); - nonlocal |= (var && (!var->isLocal() || var->isStatic()) && !var->isArgument()); - // TODO: if var is pointer check what it points at - nonlocal |= (var && (var->isPointer() || var->isReference())); + if (!nonlocal && var) { + if (!(var->isLocal() || var->isStatic() || var->isArgument())) + nonlocal = true; + else if ((var->isPointer() || var->isReference()) && !Token::simpleMatch(cond->astParent(), "!")) + // TODO: if var is pointer check what it points at + nonlocal = true; + } } else if (!nonlocal && cond->isName()) { // varid is 0. this is possibly a nonlocal variable.. nonlocal = Token::Match(cond->astParent(), "%cop%|(|["); @@ -534,7 +538,6 @@ void CheckCondition::multiCondition2() sameConditionAfterEarlyExitError(cond1, cond2); } } - } if (Token::Match(tok, "%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables break; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index ad94819cf..2a8d73bbd 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1452,6 +1452,7 @@ private: "}"); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:7]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str()); + // pointers... check("void f(struct ABC *abc) {\n" " struct AB *ab = abc->ab;\n" " if (ab->a == 123){\n" @@ -1463,6 +1464,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("void f(const int *i) {\n" + " if (!i) return;\n" + " if (!num1tok) { *num1 = *num2; }\n" + " if (!i) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str()); + { // #6095 - calling member function that might change the state check("void f() {\n"