From 014ad937cfeea492e31fad314c29583353734020 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 7 Oct 2009 23:38:21 +0300 Subject: [PATCH] Optimize speed and fix one false positive with stl checks. --- src/checkother.cpp | 2 +- src/checkstl.cpp | 10 +++++----- test/testother.cpp | 11 +++++++++++ test/teststl.cpp | 12 ++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index 3710dd41a..eddbd3202 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -948,7 +948,7 @@ void CheckOther::nullPointer() { // Make sure there is a "break" to prevent segmentation faults.. unsigned int indentlevel4 = indentlevel3; - for (const Token *tok4 = tok3; tok4; tok4 = tok4->next()) + for (const Token *tok4 = tok3->next()->link(); tok4; tok4 = tok4->next()) { if (tok4->str() == "{") ++indentlevel4; diff --git a/src/checkstl.cpp b/src/checkstl.cpp index 8f399963f..9c4783781 100644 --- a/src/checkstl.cpp +++ b/src/checkstl.cpp @@ -96,7 +96,7 @@ void CheckStl::stlOutOfBounds() continue; unsigned int indent = 0; - for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) + for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { if (tok2->str() == "(") @@ -111,18 +111,18 @@ void CheckStl::stlOutOfBounds() if (Token::Match(tok2, "; %var% <= %var% . size ( ) ;")) { - indent = 0; + unsigned int indent2 = 0; const std::string num(tok2->strAt(1)); const std::string varname(tok2->strAt(3)); for (const Token *tok3 = tok2->tokAt(8); tok3; tok3 = tok3->next()) { if (tok3->str() == "{") - ++indent; + ++indent2; else if (tok3->str() == "}") { - if (indent == 0) + if (indent2 <= 1) break; - --indent; + --indent2; } else if (tok3->str() == varname) { diff --git a/test/testother.cpp b/test/testother.cpp index 2d6342bf3..e8a94701d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -632,6 +632,17 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: tok\n", errout.str()); + checkNullPointer("void foo()\n" + "{\n" + " for (const Token *tok = tokens; tok; tok = tok->next())\n" + " {\n" + " while (tok && tok->str() != \";\")\n" + " tok = tok->next();\n" + " if( !tok ) break;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + checkNullPointer("void foo()\n" "{\n" " for (const Token *tok = tokens; tok; tok = tok ? tok->next() : NULL)\n" diff --git a/test/teststl.cpp b/test/teststl.cpp index c5a951a40..08b4085f3 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -178,6 +178,18 @@ private: " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:6]: (error) When ii==foo.size(), foo[ii] is out of bounds\n", errout.str()); + + check("void foo()\n" + "{\n" + " std::vector foo;\n" + " foo.push_back(1);\n" + " for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n" + " {\n" + " }\n" + " int ii = 0;\n" + " foo[ii] = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void STLSizeNoErr()