From dc629b4c39bdbf7a9bfe3ab230f11f8d5046ccf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 28 Jul 2011 08:12:21 +0200 Subject: [PATCH] Fixed 'possible null pointer dereference' warning messages --- lib/checkmemoryleak.cpp | 2 ++ lib/checkother.cpp | 2 ++ lib/checkother.h | 2 +- lib/checkstl.cpp | 7 +++++-- lib/checkuninitvar.cpp | 2 +- test/testother.cpp | 2 +- 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 11e4c529c..34156b4b4 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -811,6 +811,8 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::liststr() != "{")) ftok = ftok->next(); + if (!ftok) + return 0; Token *func = getcode(ftok->tokAt(1), callstack, parameterVarid, alloctype, dealloctype, false, sz); //simplifycode(func, all); const Token *func_ = func; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index efa498a93..6d2836b6c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -876,6 +876,8 @@ void CheckOther::invalidFunctionUsage() const Token *tok2 = tok->tokAt(3); while (tok2 && tok2->str() != ",") tok2 = tok2->next(); + if (!tok2) + continue; // is any source buffer overlapping the target buffer? int parlevel = 0; diff --git a/lib/checkother.h b/lib/checkother.h index 2508c9681..7cd95f77f 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -101,7 +101,7 @@ public: checkOther.checkComparisonOfBoolWithInt(); checkOther.checkSwitchCaseFallThrough(); checkOther.checkAlwaysTrueOrFalseStringCompare(); - + checkOther.checkAssignBoolToPointer(); } diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index cf00ce976..2fbd128e6 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -763,7 +763,8 @@ void CheckStl::if_find() while (decl && !Token::Match(decl, "[;{}(,]")) decl = decl->previous(); - decl = decl->next(); + if (decl) + decl = decl->next(); // stl container if (Token::Match(decl, "const| std :: %var% < %type% > &|*| %varid%", varid)) @@ -1137,8 +1138,10 @@ void CheckStl::checkAutoPointer() { tok3 = tok3->next(); } + if (!tok3) + continue; tok3 = tok3->previous()->previous(); - if (Token::Match(tok3->previous(), "[ ] )")) + if (Token::simpleMatch(tok3->previous(), "[ ] )")) { autoPointerArrayError(tok2->next()); } diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index f398d5be6..5b307052f 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1088,7 +1088,7 @@ public: } // found simple function.. - if (tok2->link() == tok->tokAt(2)) + if (tok2 && tok2->link() == tok->tokAt(2)) func.insert(tok->next()->str()); } } diff --git a/test/testother.cpp b/test/testother.cpp index 2034680c4..b0a7cbf61 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -36,7 +36,7 @@ private: void run() { TEST_CASE(assignBoolToPointer); - + TEST_CASE(zeroDiv1); TEST_CASE(zeroDiv2); TEST_CASE(zeroDiv3);