From 853d56030b8f236b1efd33378c07f981a4375b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 Apr 2014 11:27:58 +0200 Subject: [PATCH] Fixed #5559 (false positive: (error) Possible null pointer dereference: pSTRunner) --- lib/valueflow.cpp | 9 ++++++++- test/testvalueflow.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 87a4c0e9c..edbfd4c17 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -531,6 +531,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger, int indentlevel = 0; unsigned int number_of_if = 0; int varusagelevel = -1; + bool returnStatement = false; // current statement is a return, stop analysis at the ";" for (Token *tok2 = tok; tok2 && tok2 != endToken; tok2 = tok2->next()) { if (indentlevel >= 0 && tok2->str() == "{") @@ -611,12 +612,18 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger, tok2 = tok2->linkAt(2); } - else if (Token::Match(tok2, "break|continue")) { + else if (indentlevel <= 0 && Token::Match(tok2, "break|continue")) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + ". noreturn conditional scope."); break; } + else if (indentlevel <= 0 && tok2->str() == "return") + returnStatement = true; + + else if (returnStatement && tok2->str() == ";") + break; + if (tok2->varId() == varid) { // bailout: assignment if (Token::Match(tok2->previous(), "!!* %var% =")) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 16ca334be..c4cf88551 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -613,6 +613,16 @@ private: "}"; ASSERT_EQUALS(false, testValueOfX(code, 6U, 0)); + code = "void f () {\n" + " ST * x = g_pST;\n" + " if (x->y == 0) {\n" + " x = NULL;\n" + " return 1;\n" + " }\n" + " a = x->y;\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 7U, 0)); + // multivariables code = "void f(int a) {\n" " int x = a;\n"