2008-12-10 20:34:04 +01:00
|
|
|
/*
|
2008-10-26 08:55:15 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
#define UNIT_TESTING
|
2008-08-22 08:30:06 +02:00
|
|
|
#include "tokenize.h"
|
|
|
|
#include "CheckMemoryLeak.h"
|
2008-10-13 08:42:40 +02:00
|
|
|
#include "testsuite.h"
|
2008-12-09 01:49:32 +01:00
|
|
|
|
2008-11-26 19:13:36 +01:00
|
|
|
#include <iostream>
|
2008-08-22 08:30:06 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
2008-10-13 08:42:40 +02:00
|
|
|
class TestMemleak : public TestFixture
|
2008-08-22 08:30:06 +02:00
|
|
|
{
|
2008-10-13 08:42:40 +02:00
|
|
|
public:
|
|
|
|
TestMemleak() : TestFixture("TestMemleak")
|
|
|
|
{ }
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
private:
|
2008-12-10 20:15:41 +01:00
|
|
|
void check( const char code[], bool showAll = false )
|
2008-08-22 08:30:06 +02:00
|
|
|
{
|
|
|
|
// Tokenize..
|
2008-11-22 20:39:12 +01:00
|
|
|
Tokenizer tokenizer;
|
2008-11-09 11:09:42 +01:00
|
|
|
std::istringstream istr(code);
|
2008-12-10 20:15:41 +01:00
|
|
|
tokenizer.tokenize( istr, "test.cpp" );
|
2008-12-09 08:27:11 +01:00
|
|
|
tokenizer.setVarId();
|
2008-11-25 19:34:51 +01:00
|
|
|
tokenizer.simplifyTokenList();
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check for memory leaks..
|
2008-11-16 06:57:41 +01:00
|
|
|
Settings settings;
|
2008-12-09 01:49:32 +01:00
|
|
|
settings._debug = true;
|
2008-12-10 20:15:41 +01:00
|
|
|
settings._showAll = showAll;
|
2008-11-23 12:08:07 +01:00
|
|
|
tokenizer.fillFunctionList();
|
2008-11-20 23:19:26 +01:00
|
|
|
CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings, this );
|
2008-11-11 07:42:09 +01:00
|
|
|
checkMemoryLeak.CheckMemoryLeak();
|
2008-08-22 08:30:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-13 08:42:40 +02:00
|
|
|
void run()
|
2008-08-22 08:30:06 +02:00
|
|
|
{
|
2008-12-01 19:52:40 +01:00
|
|
|
TEST_CASE( simple1 );
|
2008-08-22 08:30:06 +02:00
|
|
|
TEST_CASE( simple2 );
|
|
|
|
TEST_CASE( simple3 );
|
|
|
|
TEST_CASE( simple4 );
|
|
|
|
TEST_CASE( simple5 );
|
|
|
|
TEST_CASE( simple6 );
|
|
|
|
TEST_CASE( simple7 );
|
2008-11-09 11:09:42 +01:00
|
|
|
TEST_CASE( simple8 );
|
|
|
|
|
2008-11-09 09:40:57 +01:00
|
|
|
TEST_CASE( use1 );
|
2008-11-09 11:09:42 +01:00
|
|
|
TEST_CASE( use2 );
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
TEST_CASE( ifelse1 );
|
2008-11-19 19:46:28 +01:00
|
|
|
TEST_CASE( ifelse2 );
|
2008-12-01 19:52:40 +01:00
|
|
|
TEST_CASE( ifelse3 );
|
2008-08-22 08:30:06 +02:00
|
|
|
TEST_CASE( ifelse4 );
|
|
|
|
TEST_CASE( ifelse5 );
|
2008-11-08 08:19:19 +01:00
|
|
|
TEST_CASE( ifelse6 );
|
|
|
|
TEST_CASE( ifelse7 );
|
|
|
|
TEST_CASE( ifelse8 );
|
|
|
|
TEST_CASE( ifelse9 );
|
2008-08-22 08:30:06 +02:00
|
|
|
|
2008-11-19 19:46:28 +01:00
|
|
|
TEST_CASE( if1 );
|
2008-11-22 21:00:36 +01:00
|
|
|
TEST_CASE( if2 );
|
|
|
|
TEST_CASE( if3 );
|
|
|
|
TEST_CASE( if4 );
|
|
|
|
TEST_CASE( if5 );
|
|
|
|
|
2008-11-22 12:30:50 +01:00
|
|
|
TEST_CASE( alwaysTrue );
|
2008-11-09 18:27:23 +01:00
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
TEST_CASE( forwhile1 );
|
2008-11-19 19:46:28 +01:00
|
|
|
TEST_CASE( forwhile2 );
|
2008-08-27 08:33:27 +02:00
|
|
|
TEST_CASE( forwhile3 );
|
|
|
|
TEST_CASE( forwhile4 );
|
2008-11-06 20:16:22 +01:00
|
|
|
TEST_CASE( forwhile5 );
|
2008-12-01 19:52:40 +01:00
|
|
|
TEST_CASE( forwhile6 );
|
2008-11-19 19:46:28 +01:00
|
|
|
TEST_CASE( forwhile7 );
|
2008-08-22 08:30:06 +02:00
|
|
|
|
2008-11-07 17:19:55 +01:00
|
|
|
TEST_CASE( dowhile1 );
|
|
|
|
|
2008-08-31 09:42:54 +02:00
|
|
|
TEST_CASE( switch1 );
|
2008-12-01 19:52:40 +01:00
|
|
|
TEST_CASE( switch2 );
|
2008-08-22 08:30:06 +02:00
|
|
|
|
2008-11-07 18:24:19 +01:00
|
|
|
TEST_CASE( ret1 );
|
2008-12-13 12:39:36 +01:00
|
|
|
TEST_CASE( ret2 );
|
|
|
|
TEST_CASE( ret3 );
|
2008-12-12 20:21:34 +01:00
|
|
|
TEST_CASE( ret4 );
|
2008-11-02 11:33:38 +01:00
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
TEST_CASE( mismatch1 );
|
|
|
|
|
2008-08-23 18:40:53 +02:00
|
|
|
TEST_CASE( func1 );
|
|
|
|
TEST_CASE( func2 );
|
2008-12-01 19:52:40 +01:00
|
|
|
TEST_CASE( func3 );
|
2008-11-09 18:36:53 +01:00
|
|
|
TEST_CASE( func4 );
|
2008-11-22 21:00:36 +01:00
|
|
|
TEST_CASE( func5 );
|
2008-12-01 19:52:40 +01:00
|
|
|
TEST_CASE( func6 );
|
2008-12-09 01:49:32 +01:00
|
|
|
// TODO TEST_CASE( func7 );
|
2008-12-13 12:39:36 +01:00
|
|
|
TEST_CASE( func8 ); // Using callback
|
2008-12-13 18:57:36 +01:00
|
|
|
TEST_CASE( func9 ); // Embedding the function call in a if-condition
|
2008-08-23 18:40:53 +02:00
|
|
|
|
2008-12-10 20:15:41 +01:00
|
|
|
TEST_CASE( class1 );
|
|
|
|
TEST_CASE( class2 );
|
2008-11-10 19:51:44 +01:00
|
|
|
|
2008-11-19 19:46:28 +01:00
|
|
|
TEST_CASE( throw1 );
|
2008-11-10 19:51:44 +01:00
|
|
|
|
|
|
|
TEST_CASE( linux_list_1 );
|
2008-11-16 06:57:41 +01:00
|
|
|
|
2008-12-09 01:49:32 +01:00
|
|
|
TEST_CASE( sizeof1 );
|
|
|
|
|
|
|
|
TEST_CASE( realloc1 );
|
|
|
|
TEST_CASE( realloc2 );
|
|
|
|
|
2008-12-10 20:15:41 +01:00
|
|
|
TEST_CASE( assign );
|
2008-12-12 20:21:34 +01:00
|
|
|
|
2008-12-09 08:27:11 +01:00
|
|
|
// TODO TEST_CASE( varid );
|
2008-12-13 12:19:09 +01:00
|
|
|
|
2008-12-13 17:42:39 +01:00
|
|
|
TEST_CASE( cast1 );
|
|
|
|
TEST_CASE( cast2 );
|
|
|
|
TEST_CASE( cast3 );
|
2008-08-22 08:30:06 +02:00
|
|
|
}
|
|
|
|
|
2008-11-10 19:51:44 +01:00
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
void simple1()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a = new int[10];\n"
|
2008-12-09 01:49:32 +01:00
|
|
|
"}\n" );
|
2008-08-22 08:30:06 +02:00
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: a\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void simple2()
|
|
|
|
{
|
|
|
|
check( "Fred *NewFred()\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred *f = new Fred;\n"
|
|
|
|
" return f;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void simple3()
|
|
|
|
{
|
|
|
|
check( "static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" return (char *)s;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple4()
|
|
|
|
{
|
|
|
|
check( "static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: s\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple5()
|
|
|
|
{
|
|
|
|
check( "static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct *str = new strlist;\n"
|
|
|
|
" return &str->s;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple6()
|
|
|
|
{
|
|
|
|
check( "static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" char *str2 = (char *)str;\n"
|
|
|
|
" free(str2);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple7()
|
|
|
|
{
|
|
|
|
// A garbage collector may delete f automaticly
|
|
|
|
check( "class Fred;\n"
|
|
|
|
"void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred *f = new Fred;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-19 09:23:06 +02:00
|
|
|
void simple8()
|
|
|
|
{
|
|
|
|
check( "char * foo ()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"abc\");\n"
|
|
|
|
" if (somecondition)\n"
|
|
|
|
" for (i = 0; i < 2; i++)\n"
|
|
|
|
" { }\n"
|
|
|
|
" return str;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
|
2008-11-09 11:09:42 +01:00
|
|
|
|
|
|
|
void use1()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
2008-11-22 21:00:36 +01:00
|
|
|
" char *str;\n"
|
|
|
|
" if (somecondition)\n"
|
2008-11-17 18:31:07 +01:00
|
|
|
" str = strdup(\"abc\");\n"
|
2008-11-09 11:09:42 +01:00
|
|
|
" if (somecondition)\n"
|
|
|
|
" DeleteString(str);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
2008-11-17 18:31:07 +01:00
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
2008-11-09 11:09:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void use2()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"abc\");\n"
|
2008-11-17 18:31:07 +01:00
|
|
|
" if ( abc ) { memset(str, 0, 3); }\n"
|
2008-11-09 11:09:42 +01:00
|
|
|
" *somestr = str;\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
2008-11-08 08:19:19 +01:00
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
void ifelse1()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a = new int[10];\n"
|
|
|
|
" if (a)\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] a;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse2()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" if (somecondition)\n"
|
|
|
|
" {\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" free(str);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse3()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" if (a==b)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:9]: Memory leak: str\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse4()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[10];\n"
|
|
|
|
" if (a==b)\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse5()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str;\n"
|
|
|
|
" if (somecondition)\n"
|
|
|
|
" {\n"
|
|
|
|
" str = new char[100];\n"
|
|
|
|
" }\n"
|
|
|
|
" else\n"
|
|
|
|
" {\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse6()
|
|
|
|
{
|
|
|
|
check( "static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" if ( a == b )\n"
|
|
|
|
" {\n"
|
|
|
|
" return s;\n"
|
|
|
|
" }\n"
|
|
|
|
" return NULL;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:8]: Memory leak: s\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse7()
|
|
|
|
{
|
|
|
|
check( "static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" if ( abc )\n"
|
|
|
|
" {\n"
|
|
|
|
" s = new char[10];\n"
|
|
|
|
" }\n"
|
|
|
|
" return s;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse8()
|
|
|
|
{
|
|
|
|
check( "static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[10];\n"
|
|
|
|
" if ( s )\n"
|
|
|
|
" {\n"
|
|
|
|
" return s;\n"
|
|
|
|
" }\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-08 08:19:19 +01:00
|
|
|
void ifelse9()
|
|
|
|
{
|
|
|
|
check( "static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[10];\n"
|
|
|
|
" if ( ghfgf )\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] s;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n" );
|
2008-11-17 18:31:07 +01:00
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
2008-11-08 08:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
|
2008-11-09 18:27:23 +01:00
|
|
|
void if1()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct abc *p = new abc;\n"
|
|
|
|
" p->a = new char[100];\n"
|
|
|
|
" if ( ! p->a )\n"
|
|
|
|
" return;\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: p\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
2008-11-16 06:57:41 +01:00
|
|
|
void if2()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct smp_alt_module *smp;\n"
|
|
|
|
" smp = kzalloc(sizeof(*smp), GFP_KERNEL);\n"
|
|
|
|
" if (NULL == smp)\n"
|
|
|
|
" return;\n"
|
|
|
|
" kfree( smp );\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
2008-11-22 21:00:36 +01:00
|
|
|
|
|
|
|
void if3()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" if (0 != s)\n"
|
|
|
|
" foo(s);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void if4()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" bool b = true;\n"
|
|
|
|
" if (b && (s = malloc(256)))\n"
|
|
|
|
" ;\n"
|
|
|
|
" if (b)\n"
|
|
|
|
" free(s);\n"
|
|
|
|
"}\n" );
|
2008-12-01 19:52:40 +01:00
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if5()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = malloc(256);\n"
|
|
|
|
" if (somecondition && !p)\n"
|
|
|
|
" return;\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
std::string err( errout.str() );
|
|
|
|
ASSERT_EQUALS( std::string(""), err );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void alwaysTrue()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = 0;\n"
|
|
|
|
" for (;;)\n"
|
|
|
|
" {\n"
|
|
|
|
" p = malloc(256);\n"
|
|
|
|
" if (1)\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
std::string err( errout.str() );
|
|
|
|
ASSERT_EQUALS( std::string(""), err );
|
|
|
|
}
|
2008-11-09 18:27:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
void forwhile1()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" while (condition)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (condition)\n"
|
|
|
|
" {\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" free(str);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void forwhile2()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for (int i = 0; i < j; i++)\n"
|
|
|
|
" {\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" if (condition)\n"
|
|
|
|
" continue;\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:7]: Memory leak: str\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-27 08:33:27 +02:00
|
|
|
void forwhile3()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = 0;\n"
|
|
|
|
" for (int i = 0; i < 10; i++)\n"
|
|
|
|
" {\n"
|
|
|
|
" str = strdup(\"hello\");\n"
|
|
|
|
" }\n"
|
|
|
|
" free(str);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: str\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
2008-08-27 08:33:27 +02:00
|
|
|
void forwhile4()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = 0;\n"
|
|
|
|
" for (int i = 0; i < 10; i++)\n"
|
|
|
|
" {\n"
|
|
|
|
" str = strdup(\"hello\");\n"
|
|
|
|
" if (str) { }\n"
|
|
|
|
" }\n"
|
|
|
|
" free(str);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: str\n"), errout.str() );
|
|
|
|
}
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
|
2008-11-06 20:16:22 +01:00
|
|
|
void forwhile5()
|
|
|
|
{
|
|
|
|
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"
|
|
|
|
" return str;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 08:46:28 +01:00
|
|
|
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() );
|
|
|
|
}
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
2008-11-10 21:30:47 +01:00
|
|
|
void forwhile7()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for (int i = 0; i < j; i++)\n"
|
|
|
|
" {\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" if (condition)\n"
|
|
|
|
" break;\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:7]: Memory leak: str\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
|
2008-11-07 17:19:55 +01:00
|
|
|
void dowhile1()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"abc\");\n"
|
|
|
|
" do\n"
|
|
|
|
" {\n"
|
|
|
|
" str = strdup(\"def\");\n"
|
|
|
|
" }\n"
|
|
|
|
" while (!str);\n"
|
|
|
|
" return str;\n"
|
2008-12-09 01:49:32 +01:00
|
|
|
"}\n" );
|
|
|
|
std::string err( errout.str() );
|
2008-11-30 20:00:07 +01:00
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: str\n"), err );
|
2008-11-07 17:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-22 08:30:06 +02:00
|
|
|
void switch1()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[10];\n"
|
|
|
|
" switch (abc)\n"
|
|
|
|
" {\n"
|
|
|
|
" case 1:\n"
|
|
|
|
" break;\n"
|
|
|
|
" };\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void switch2()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[10];\n"
|
|
|
|
" switch (abc)\n"
|
|
|
|
" {\n"
|
|
|
|
" case 1:\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
" break;\n"
|
|
|
|
" default:\n"
|
|
|
|
" break;\n"
|
|
|
|
" };\n"
|
|
|
|
"}\n");
|
2008-08-25 20:01:11 +02:00
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:12]: Memory leak: str\n"), errout.str() );
|
2008-08-22 08:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-11-07 18:24:19 +01:00
|
|
|
void ret1()
|
2008-11-02 11:33:38 +01:00
|
|
|
{
|
|
|
|
check( "char *f( char **str )\n"
|
|
|
|
"{\n"
|
|
|
|
" char *ret = malloc( 10 );\n"
|
|
|
|
" return *str = ret;\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 18:24:19 +01:00
|
|
|
void ret2()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct ABC *abc = new ABC;\n"
|
|
|
|
" abc->a = new char[10];\n"
|
|
|
|
" if ( ! abc->a )\n"
|
|
|
|
" return;\n"
|
|
|
|
" delete [] abc->a;\n"
|
|
|
|
" delete abc;\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: abc\n"), errout.str() );
|
|
|
|
}
|
2008-11-02 11:33:38 +01:00
|
|
|
|
2008-12-13 12:39:36 +01:00
|
|
|
void ret3()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *filep = fopen(\"myfile.txt\",\"w\");\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Resource leak: filep\n"), errout.str() );
|
2008-12-12 20:21:34 +01:00
|
|
|
}
|
2008-12-13 12:39:36 +01:00
|
|
|
|
|
|
|
void ret4()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *p = popen( \"ls -l\", \"r\");\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Resource leak: p\n"), errout.str() );
|
2008-12-12 20:21:34 +01:00
|
|
|
}
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
void mismatch1()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
2008-08-23 18:40:53 +02:00
|
|
|
"{\n"
|
|
|
|
" int *a = new int[10];\n"
|
|
|
|
" free(a);\n"
|
|
|
|
"}\n");
|
2008-08-22 08:30:06 +02:00
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Mismatching allocation and deallocation: a\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-23 18:40:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
// function calls
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
void func1()
|
|
|
|
{
|
|
|
|
check( "static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func2()
|
|
|
|
{
|
|
|
|
check( "static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo.add(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-09 17:34:18 +01:00
|
|
|
void func3()
|
|
|
|
{
|
|
|
|
check( "static void foo(const char *str)\n"
|
|
|
|
"{ }\n"
|
|
|
|
"\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:8]: Memory leak: p\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-09 18:36:53 +01:00
|
|
|
void func4()
|
|
|
|
{
|
|
|
|
check( "static void foo(char *str)\n"
|
|
|
|
"{\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-11 21:03:06 +01:00
|
|
|
void func5()
|
|
|
|
{
|
|
|
|
check( "static void foo(char *str)\n"
|
|
|
|
"{\n"
|
|
|
|
" delete str;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo(p);\n"
|
2008-11-22 21:00:36 +01:00
|
|
|
"}\n" );
|
|
|
|
std::string err( errout.str() );
|
2008-11-16 14:55:16 +01:00
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:9] -> [test.cpp:3]: Mismatching allocation and deallocation: str\n"), err );
|
2008-11-11 21:03:06 +01:00
|
|
|
}
|
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
|
|
|
|
void func6()
|
|
|
|
{
|
|
|
|
check( "static void foo(char *str)\n"
|
|
|
|
"{\n"
|
|
|
|
" goto abc;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
std::string err( errout.str() );
|
2008-11-26 19:13:36 +01:00
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:10]: Memory leak: p\n"), err );
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
|
|
|
|
2008-12-09 01:49:32 +01:00
|
|
|
|
|
|
|
void func7()
|
|
|
|
{
|
|
|
|
check( "static void foo(char *str)\n"
|
|
|
|
"{\n"
|
|
|
|
" if (abc)\n"
|
|
|
|
" return;"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
std::string err( errout.str() );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:11]: Memory leak: p\n"), err );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func8()
|
|
|
|
{
|
|
|
|
check( "static void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[100];"
|
|
|
|
" (*release)(str);\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
2008-12-13 12:39:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
void func9()
|
|
|
|
{
|
|
|
|
check( "int b()\n"
|
|
|
|
"{\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void a()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = new char[10];\n"
|
|
|
|
" if (b())\n"
|
|
|
|
" return;\n"
|
|
|
|
" delete [] a;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
2008-12-13 11:01:05 +01:00
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
|
2008-12-09 00:06:19 +01:00
|
|
|
|
2008-08-23 18:40:53 +02:00
|
|
|
/*
|
2008-10-08 09:01:25 +02:00
|
|
|
void func3()
|
|
|
|
{
|
|
|
|
check( "static char *dmalloc()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" return p;\n"
|
|
|
|
"}\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = dmalloc();\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:9]: Memory leak: p\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func4()
|
|
|
|
{
|
|
|
|
check( "static char *dmalloc()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" return p;\n"
|
|
|
|
"}\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = dmalloc();\n"
|
|
|
|
" delete p;\n"
|
|
|
|
"}\n" );
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:9]: Mismatching allocation and deallocation: p\n"), errout.str() );
|
|
|
|
}
|
2008-10-15 08:48:18 +02:00
|
|
|
*/
|
2008-08-23 18:40:53 +02:00
|
|
|
|
2008-09-01 08:38:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void class1()
|
|
|
|
{
|
|
|
|
check( "class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" char *str1;\n"
|
|
|
|
" char *str2;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" ~Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" str1 = new char[10];\n"
|
|
|
|
" str2 = new char[10];\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"Fred::~Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" delete [] str2;\n"
|
2008-12-10 20:15:41 +01:00
|
|
|
"}\n", true );
|
2008-09-01 08:38:41 +02:00
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:1]: Memory leak: Fred::str1\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void class2()
|
|
|
|
{
|
|
|
|
check( "class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" char *str1;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" ~Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" str1 = new char[10];\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"Fred::~Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" free(str1);\n"
|
2008-12-10 20:15:41 +01:00
|
|
|
"}\n", true );
|
2008-09-01 08:38:41 +02:00
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:17]: Mismatching allocation and deallocation: Fred::str1\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-11-10 19:51:44 +01:00
|
|
|
void throw1()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[10];\n"
|
|
|
|
" if ( ! abc )\n"
|
|
|
|
" throw 123;\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: str\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void linux_list_1()
|
|
|
|
{
|
|
|
|
check( "struct AB\n"
|
|
|
|
"{\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int b;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct AB *ab = new AB;\n"
|
|
|
|
" func(&ab->a);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
2008-11-16 06:57:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
void sizeof1()
|
|
|
|
{
|
|
|
|
check( "void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct s_t s1;\n"
|
|
|
|
" struct s_t cont *p = &s1;\n"
|
|
|
|
" struct s_t *s2;\n"
|
|
|
|
"\n"
|
|
|
|
" memset(p, 0, sizeof(*p));\n"
|
|
|
|
"\n"
|
|
|
|
" s2 = (struct s_t *) malloc(sizeof(*s2));\n"
|
|
|
|
"\n"
|
|
|
|
" if (s2->value != 0)\n"
|
|
|
|
" return;\n"
|
|
|
|
"\n"
|
|
|
|
" free(s2);\n"
|
|
|
|
"\n"
|
|
|
|
" return;\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
std::string err( errout.str() );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:12]: Memory leak: s2\n"), err );
|
|
|
|
}
|
2008-12-09 01:49:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
void realloc1()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = realloc(a, 100);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: a\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void realloc2()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = (char *)realloc(a, 100);\n"
|
|
|
|
" free(a);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void assign()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = 0;\n"
|
|
|
|
" free(a);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:3]: Memory leak: a\n"), errout.str() );
|
|
|
|
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" char *p = a;\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a += 10;\n"
|
|
|
|
" free(a - 10);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
2008-12-13 12:39:36 +01:00
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = (void *)a + 10;\n"
|
|
|
|
" free(a - 10);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
2008-12-09 01:49:32 +01:00
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
2008-12-10 20:15:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
void varid()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" {\n"
|
|
|
|
" char *p = 0;\n"
|
|
|
|
" delete p;\n"
|
|
|
|
" }\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n" );
|
2008-12-09 08:27:11 +01:00
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
2008-12-10 20:15:41 +01:00
|
|
|
}
|
2008-11-16 06:57:41 +01:00
|
|
|
|
2008-12-13 12:19:09 +01:00
|
|
|
void cast1()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = reinterpret_cast<char *>(malloc(10));\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: a\n"), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void cast2()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = malloc(10);\n"
|
|
|
|
" free((void *)a);\n"
|
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void cast3()
|
|
|
|
{
|
|
|
|
check( "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = malloc(10);\n"
|
2008-12-13 17:42:39 +01:00
|
|
|
" free(reinterpret_cast<void *>(a));\n"
|
2008-12-13 12:19:09 +01:00
|
|
|
"}\n" );
|
|
|
|
|
|
|
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
|
|
|
}
|
2008-08-22 08:30:06 +02:00
|
|
|
};
|
|
|
|
|
2008-10-16 19:22:26 +02:00
|
|
|
REGISTER_TEST( TestMemleak )
|
2008-08-22 08:30:06 +02:00
|
|
|
|
|
|
|
|