Memory Leaks: Improved the checking of loops.

This commit is contained in:
Daniel Marjamäki 2008-11-07 07:46:28 +00:00
parent 53752c129d
commit cdf9ee4d8a
3 changed files with 31 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* /*
* c++check - c/c++ syntax checking * c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki * Copyright (C) 2007 Daniel Marjamäki
* *
@ -498,6 +498,20 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
done = false; done = false;
} }
// Replace "loop !var ;" with ";"
if ( Match(tok2->next, "loop !var ;") )
{
erase(tok2, gettok(tok2,4));
done = false;
}
// Replace "loop !var alloc ;" with " alloc ;"
if ( Match(tok2->next, "loop !var alloc ;") )
{
erase(tok2, gettok(tok2,3));
done = false;
}
// Delete if block in "alloc ; if(!var) return ;" // Delete if block in "alloc ; if(!var) return ;"
if ( Match(tok2, "alloc ; if(!var) return ;") ) if ( Match(tok2, "alloc ; if(!var) return ;") )
{ {

View File

@ -1,4 +1,4 @@
/* /*
* c++check - c/c++ syntax checking * c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki * Copyright (C) 2007 Daniel Marjamäki
* *

View File

@ -1,4 +1,4 @@
/* /*
* c++check - c/c++ syntax checking * c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki * Copyright (C) 2007 Daniel Marjamäki
* *
@ -75,6 +75,7 @@ private:
TEST_CASE( forwhile3 ); TEST_CASE( forwhile3 );
TEST_CASE( forwhile4 ); TEST_CASE( forwhile4 );
TEST_CASE( forwhile5 ); TEST_CASE( forwhile5 );
TEST_CASE( forwhile6 );
TEST_CASE( switch1 ); TEST_CASE( switch1 );
TEST_CASE( switch2 ); TEST_CASE( switch2 );
@ -390,6 +391,19 @@ private:
} }
void forwhile6()
{
check( "void f(const char **a)\n"
"{\n"
" char *str = 0;\n"
" for (int i = 0; i < 10 && !str; ++i)\n"
" {\n"
" str = strdup(a[i]);\n"
" }\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:8]: Memory leak: str\n"), errout.str() );
}