From d5442280b10d99743786d8808e18eaaba231686b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 1 May 2012 07:06:14 +0200 Subject: [PATCH] Fixed #3597 (Errors in evaluation of chained assignment operators) --- lib/checknullpointer.cpp | 19 ++++++++++++++++++- test/testnullpointer.cpp | 8 ++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 24e28915f..c8224b3bc 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -863,7 +863,24 @@ void CheckNullPointer::nullPointerByDeRefAndChec() // uncertain bool unknown = _settings->inconclusive; - if (Token::Match(tok1->tokAt(-2), "%varid% = %varid% .", varid)) { + // reassign : is the pointer reassigned like this: + // tok = tok->next(); + bool reassign = false; + if (Token::Match(tok1->previous(), "= %varid% .", varid)) { + const Token *back = tok1->tokAt(-2); + while (back) { + if (back->varId() == varid) { + reassign = true; + break; + } + if (Token::Match(back, "[{};,(]")) { + break; + } + back = back->previous(); + } + } + + if (reassign) { break; } else if (Token::simpleMatch(tok1->tokAt(-2), "* )") && Token::Match(tok1->linkAt(-1)->tokAt(-2), "%varid% = (", tok1->varId())) { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 520a946af..41846aef8 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -653,6 +653,14 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void foo(x *p)\n" + "{\n" + " p = *p2 = p->next;\n" + " if (!p)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("void foo(struct ABC *abc)\n" "{\n" " abc = abc ? abc->next : 0;\n"