Optimize speed and fix one false positive with stl checks.

This commit is contained in:
Reijo Tomperi 2009-10-07 23:38:21 +03:00
parent 52ca36a6ed
commit 014ad937cf
4 changed files with 29 additions and 6 deletions

View File

@ -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;

View File

@ -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)
{

View File

@ -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"

View File

@ -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<int> 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()