diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index ce743d668..d8a7edb6e 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -1,4 +1,4 @@ -/* +/* * c++check - c/c++ syntax checking * Copyright (C) 2007 Daniel Marjamäki * @@ -498,6 +498,20 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] 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 ;" if ( Match(tok2, "alloc ; if(!var) return ;") ) { diff --git a/CheckMemoryLeak.h b/CheckMemoryLeak.h index 6f69a7ba9..44414cfad 100644 --- a/CheckMemoryLeak.h +++ b/CheckMemoryLeak.h @@ -1,4 +1,4 @@ -/* +/* * c++check - c/c++ syntax checking * Copyright (C) 2007 Daniel Marjamäki * diff --git a/testmemleak.cpp b/testmemleak.cpp index 1217f4365..56cc51c55 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -1,4 +1,4 @@ -/* +/* * c++check - c/c++ syntax checking * Copyright (C) 2007 Daniel Marjamäki * @@ -75,6 +75,7 @@ private: TEST_CASE( forwhile3 ); TEST_CASE( forwhile4 ); TEST_CASE( forwhile5 ); + TEST_CASE( forwhile6 ); TEST_CASE( switch1 ); 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() ); + } +