2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 18:51:17 +02:00
|
|
|
#define UNIT_TESTING
|
2009-01-06 15:18:36 +01:00
|
|
|
#include "../src/tokenize.h"
|
|
|
|
#include "../src/checkmemoryleak.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
#include "testsuite.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
|
|
|
|
class TestMemleak : private TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestMemleak() : TestFixture("TestMemleak")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
TEST_CASE(testFunctionReturnType);
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckMemoryLeak::AllocType functionReturnType(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
|
|
|
|
return ((const CheckMemoryLeak *)0)->functionReturnType(tokenizer.tokens());
|
|
|
|
}
|
|
|
|
|
|
|
|
void testFunctionReturnType()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const char code[] = "const char *foo()\n"
|
|
|
|
"{ return 0; }";
|
|
|
|
ASSERT_EQUALS(CheckMemoryLeak::No, functionReturnType(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code[] = "Fred *newFred()\n"
|
|
|
|
"{ return new Fred; }";
|
|
|
|
ASSERT_EQUALS(CheckMemoryLeak::New, functionReturnType(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char code[] = "char *foo()\n"
|
|
|
|
"{ return new char[100]; }";
|
|
|
|
ASSERT_EQUALS(CheckMemoryLeak::NewArray, functionReturnType(code));
|
|
|
|
}
|
2009-06-15 21:13:39 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const char code[] = "char *foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" return p;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(CheckMemoryLeak::NewArray, functionReturnType(code));
|
|
|
|
}
|
2009-06-15 17:44:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static TestMemleak testMemleak;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
class TestMemleakInFunction : public TestFixture
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
public:
|
2009-06-08 20:20:43 +02:00
|
|
|
TestMemleakInFunction() : TestFixture("TestMemleakInFunction")
|
2008-12-18 22:28:57 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
2009-01-05 16:49:57 +01:00
|
|
|
void check(const char code[], bool showAll = false)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
2009-01-05 16:49:57 +01:00
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
2008-12-18 22:28:57 +01:00
|
|
|
tokenizer.setVarId();
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check for memory leaks..
|
|
|
|
Settings settings;
|
|
|
|
settings._debug = true;
|
|
|
|
settings._showAll = showAll;
|
|
|
|
tokenizer.fillFunctionList();
|
2009-06-08 20:20:43 +02:00
|
|
|
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, this);
|
|
|
|
checkMemoryLeak.check();
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(simple1);
|
|
|
|
TEST_CASE(simple2);
|
|
|
|
TEST_CASE(simple3);
|
|
|
|
TEST_CASE(simple4);
|
|
|
|
TEST_CASE(simple5);
|
|
|
|
TEST_CASE(simple6);
|
|
|
|
TEST_CASE(simple7);
|
|
|
|
TEST_CASE(simple8);
|
|
|
|
TEST_CASE(simple9); // Bug 2435468 - member function "free"
|
|
|
|
TEST_CASE(simple10); // fclose in a if condition
|
2009-06-17 21:42:49 +02:00
|
|
|
TEST_CASE(simple11);
|
2009-07-06 12:33:31 +02:00
|
|
|
TEST_CASE(simple12);
|
2009-06-05 06:03:48 +02:00
|
|
|
TEST_CASE(new_nothrow);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
2009-07-06 12:20:13 +02:00
|
|
|
TEST_CASE(staticvar);
|
|
|
|
|
2009-02-07 10:44:57 +01:00
|
|
|
TEST_CASE(alloc_alloc_1);
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(use1);
|
|
|
|
TEST_CASE(use2);
|
|
|
|
|
|
|
|
TEST_CASE(ifelse1);
|
|
|
|
TEST_CASE(ifelse2);
|
|
|
|
TEST_CASE(ifelse3);
|
|
|
|
TEST_CASE(ifelse4);
|
|
|
|
TEST_CASE(ifelse5);
|
|
|
|
TEST_CASE(ifelse6);
|
|
|
|
TEST_CASE(ifelse7);
|
|
|
|
TEST_CASE(ifelse8);
|
|
|
|
TEST_CASE(ifelse9);
|
2009-02-25 20:20:23 +01:00
|
|
|
TEST_CASE(ifelse10);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(if1);
|
|
|
|
TEST_CASE(if2);
|
|
|
|
TEST_CASE(if3);
|
|
|
|
TEST_CASE(if4);
|
|
|
|
TEST_CASE(if5);
|
|
|
|
TEST_CASE(if6); // Bug 2432631
|
|
|
|
TEST_CASE(if7); // Bug 2401436
|
|
|
|
TEST_CASE(if8); // Bug 2458532
|
2009-03-25 18:31:40 +01:00
|
|
|
TEST_CASE(if9); // if (realloc)
|
2009-03-28 20:50:09 +01:00
|
|
|
TEST_CASE(if10); // else if (realloc)
|
2009-02-04 07:11:36 +01:00
|
|
|
TEST_CASE(if11);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(forwhile1);
|
|
|
|
TEST_CASE(forwhile2);
|
|
|
|
TEST_CASE(forwhile3);
|
|
|
|
TEST_CASE(forwhile4);
|
|
|
|
TEST_CASE(forwhile5);
|
|
|
|
TEST_CASE(forwhile6);
|
|
|
|
TEST_CASE(forwhile7);
|
2009-03-28 20:50:09 +01:00
|
|
|
TEST_CASE(forwhile8); // Bug 2429936
|
2009-04-05 01:55:45 +02:00
|
|
|
TEST_CASE(forwhile9);
|
|
|
|
TEST_CASE(forwhile10);
|
2009-06-20 13:58:30 +02:00
|
|
|
TEST_CASE(forwhile11);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(dowhile1);
|
|
|
|
|
|
|
|
TEST_CASE(switch1);
|
|
|
|
TEST_CASE(switch2);
|
2009-01-17 08:55:40 +01:00
|
|
|
TEST_CASE(switch3);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(ret1);
|
|
|
|
TEST_CASE(ret2);
|
|
|
|
TEST_CASE(ret3);
|
|
|
|
TEST_CASE(ret4);
|
|
|
|
TEST_CASE(ret5); // Bug 2458436 - return use
|
2009-02-24 07:23:21 +01:00
|
|
|
TEST_CASE(ret6);
|
2009-03-03 21:39:17 +01:00
|
|
|
TEST_CASE(ret7);
|
2009-06-05 08:56:46 +02:00
|
|
|
TEST_CASE(ret8);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(mismatch1);
|
2009-02-06 07:11:47 +01:00
|
|
|
TEST_CASE(mismatch2);
|
|
|
|
TEST_CASE(mismatch3);
|
2009-02-02 06:58:50 +01:00
|
|
|
TEST_CASE(mismatch4);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(func1);
|
|
|
|
TEST_CASE(func2);
|
|
|
|
TEST_CASE(func3);
|
|
|
|
TEST_CASE(func4);
|
|
|
|
TEST_CASE(func5);
|
|
|
|
TEST_CASE(func6);
|
2009-03-25 18:31:40 +01:00
|
|
|
TEST_CASE(func7);
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(func8); // Using callback
|
|
|
|
TEST_CASE(func9); // Embedding the function call in a if-condition
|
|
|
|
TEST_CASE(func10); // Bug 2458510 - Function pointer
|
|
|
|
TEST_CASE(func11); // Bug 2458510 - Function pointer
|
|
|
|
TEST_CASE(func12);
|
2009-01-24 19:55:56 +01:00
|
|
|
TEST_CASE(func13);
|
2009-06-12 17:31:29 +02:00
|
|
|
TEST_CASE(func14);
|
2009-06-16 19:05:05 +02:00
|
|
|
TEST_CASE(func15);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
TEST_CASE(allocfunc1);
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(throw1);
|
|
|
|
TEST_CASE(throw2);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(linux_list_1);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(sizeof1);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(realloc1);
|
2009-03-28 20:50:09 +01:00
|
|
|
TEST_CASE(realloc2);
|
2009-03-23 18:34:59 +01:00
|
|
|
TEST_CASE(realloc3);
|
2009-03-28 20:50:09 +01:00
|
|
|
TEST_CASE(realloc4);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(assign);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-03-25 18:31:40 +01:00
|
|
|
TEST_CASE(varid);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(cast1);
|
|
|
|
TEST_CASE(cast2);
|
|
|
|
TEST_CASE(cast3);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
2009-03-25 18:31:40 +01:00
|
|
|
TEST_CASE(structmember1);
|
2008-12-31 10:02:45 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(dealloc_use_1); // Deallocate and then use memory
|
|
|
|
TEST_CASE(dealloc_use_2); // Deallocate and then use memory. No error if "use" is &var
|
|
|
|
TEST_CASE(dealloc_use_3); // Deallocate and then use memory. No error
|
|
|
|
TEST_CASE(dealloc_use_4);
|
|
|
|
TEST_CASE(dealloc_use_5);
|
|
|
|
TEST_CASE(dealloc_use_6);
|
2009-02-05 21:17:01 +01:00
|
|
|
TEST_CASE(dealloc_use_7);
|
2009-01-15 21:34:39 +01:00
|
|
|
|
|
|
|
// free a free'd pointer
|
2009-01-16 17:29:41 +01:00
|
|
|
TEST_CASE(freefree1);
|
|
|
|
TEST_CASE(freefree2);
|
2009-06-06 20:55:16 +02:00
|
|
|
TEST_CASE(strcpy_result_assignment);
|
2009-01-25 21:57:34 +01:00
|
|
|
TEST_CASE(strcat_result_assignment);
|
2009-01-31 14:57:27 +01:00
|
|
|
|
|
|
|
TEST_CASE(all1); // Extra checking when --all is given
|
2009-02-07 11:54:39 +01:00
|
|
|
|
|
|
|
TEST_CASE(malloc_constant_1); // Check that the malloc constant matches the type
|
2009-02-08 09:21:15 +01:00
|
|
|
|
|
|
|
// Calls to unknown functions.. they may throw exception, quit the program, etc
|
2009-02-08 12:59:04 +01:00
|
|
|
TEST_CASE(unknownFunction1);
|
2009-02-08 19:27:09 +01:00
|
|
|
TEST_CASE(unknownFunction2);
|
2009-02-08 19:31:09 +01:00
|
|
|
TEST_CASE(unknownFunction3);
|
2009-06-21 13:48:39 +02:00
|
|
|
TEST_CASE(unknownFunction4);
|
2009-06-23 18:29:43 +02:00
|
|
|
TEST_CASE(unknownFunction5);
|
2009-02-20 07:28:18 +01:00
|
|
|
|
|
|
|
// VCL..
|
|
|
|
TEST_CASE(vcl1);
|
2009-02-23 20:32:54 +01:00
|
|
|
TEST_CASE(vcl2);
|
2009-04-28 20:01:35 +02:00
|
|
|
|
|
|
|
TEST_CASE(autoptr1);
|
2009-05-01 11:28:33 +02:00
|
|
|
TEST_CASE(if_with_and);
|
2009-05-06 20:20:25 +02:00
|
|
|
TEST_CASE(assign_pclose);
|
2009-05-10 08:01:38 +02:00
|
|
|
|
|
|
|
// Using the function "exit"
|
|
|
|
TEST_CASE(exit1);
|
|
|
|
TEST_CASE(exit2);
|
2009-05-12 21:04:49 +02:00
|
|
|
TEST_CASE(stdstring);
|
2009-05-19 22:29:10 +02:00
|
|
|
|
|
|
|
TEST_CASE(strndup_function);
|
2009-06-01 12:40:24 +02:00
|
|
|
TEST_CASE(tmpfile_function);
|
2009-05-22 16:47:40 +02:00
|
|
|
TEST_CASE(fcloseall_function);
|
2009-05-23 13:01:18 +02:00
|
|
|
TEST_CASE(file_functions);
|
2009-05-22 09:24:03 +02:00
|
|
|
|
2009-06-21 17:01:43 +02:00
|
|
|
TEST_CASE(open_function);
|
|
|
|
TEST_CASE(creat_function);
|
|
|
|
TEST_CASE(close_function);
|
|
|
|
TEST_CASE(fd_functions);
|
|
|
|
|
2009-05-22 09:24:03 +02:00
|
|
|
TEST_CASE(opendir_function);
|
|
|
|
TEST_CASE(fdopendir_function);
|
|
|
|
TEST_CASE(closedir_function);
|
|
|
|
TEST_CASE(dir_functions);
|
|
|
|
|
2009-05-22 22:36:03 +02:00
|
|
|
TEST_CASE(pointer_to_pointer);
|
2009-05-27 21:16:54 +02:00
|
|
|
TEST_CASE(dealloc_and_alloc_in_func);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void simple1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a = new int[10];\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str());
|
2009-06-14 19:32:34 +02:00
|
|
|
|
|
|
|
// ticket #346
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int * const a = new int[10];\n"
|
|
|
|
" const int * const b = new int[10];\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n[test.cpp:5]: (error) Memory leak: b\n",
|
|
|
|
errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void simple2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("Fred *NewFred()\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred *f = new Fred;\n"
|
|
|
|
" return f;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void simple3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" return (char *)s;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: s\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct *str = new strlist;\n"
|
|
|
|
" return &str->s;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple6()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" char *str2 = (char *)str;\n"
|
|
|
|
" free(str2);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple7()
|
|
|
|
{
|
|
|
|
// A garbage collector may delete f automaticly
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred;\n"
|
|
|
|
"void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred *f = new Fred;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple8()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple9()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" MyClass *c = new MyClass();\n"
|
|
|
|
" c->free(c);\n"
|
|
|
|
" delete c;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 20:57:50 +01:00
|
|
|
void simple10()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE * f = fopen(fname, str);\n"
|
|
|
|
" if ( fclose(f) != NULL );\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-28 20:57:50 +01:00
|
|
|
}
|
|
|
|
|
2009-06-17 21:42:49 +02:00
|
|
|
void simple11()
|
|
|
|
{
|
|
|
|
check("void Fred::aaa()\n"
|
|
|
|
"{ }\n"
|
|
|
|
"\n"
|
|
|
|
"void Fred::foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = NULL;\n"
|
|
|
|
" if (a)\n"
|
|
|
|
" s = malloc(10);\n"
|
|
|
|
" else if (b)\n"
|
|
|
|
" s = malloc(10);\n"
|
|
|
|
" else\n"
|
|
|
|
" f();\n"
|
|
|
|
" g(s);\n"
|
|
|
|
" if (c)\n"
|
|
|
|
" h(s);\n"
|
|
|
|
" free(s);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-07-06 12:33:31 +02:00
|
|
|
void simple12()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" foo(s);\n"
|
|
|
|
" s = malloc(100);\n"
|
|
|
|
"}\n");
|
2009-07-06 12:38:04 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: s\n", errout.str());
|
2009-07-06 12:33:31 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 21:42:49 +02:00
|
|
|
|
|
|
|
|
2009-06-05 06:03:48 +02:00
|
|
|
void new_nothrow()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new(std::nothrow) int;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
|
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" using std::nothrow;\n"
|
|
|
|
" int *p = new(nothrow) int;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 06:03:48 +02:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new(std::nothrow) int[10];\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 06:03:48 +02:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" using namespace std;\n"
|
|
|
|
" int *p = new(nothrow) int[10];\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
|
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new(std::nothrow) int;\n"
|
|
|
|
" delete [] p;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: p\n", errout.str());
|
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new(std::nothrow) int[10];\n"
|
|
|
|
" delete p;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: p\n", errout.str());
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-07-06 12:20:13 +02:00
|
|
|
|
|
|
|
void staticvar()
|
|
|
|
{
|
|
|
|
check("int f()\n"
|
|
|
|
"{\n"
|
|
|
|
" static char *s = 0;\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = malloc(100);\n"
|
|
|
|
" return 123;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-07 10:44:57 +01:00
|
|
|
void alloc_alloc_1()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str;\n"
|
|
|
|
" str = new char[10];\n"
|
|
|
|
" str = new char[20];\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: str\n", errout.str());
|
2009-02-07 10:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void use1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str;\n"
|
|
|
|
" if (somecondition)\n"
|
|
|
|
" str = strdup(\"abc\");\n"
|
|
|
|
" if (somecondition)\n"
|
|
|
|
" DeleteString(str);\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void use2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"abc\");\n"
|
|
|
|
" if ( abc ) { memset(str, 0, 3); }\n"
|
|
|
|
" *somestr = str;\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a = new int[10];\n"
|
|
|
|
" if (a)\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] a;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" if (somecondition)\n"
|
|
|
|
" {\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" free(str);\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" if (a==b)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"hello\");\n"
|
|
|
|
" if (a==b)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n", true);
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:9]: (all) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse6()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: s\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse7()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" if ( abc )\n"
|
|
|
|
" {\n"
|
|
|
|
" s = new char[10];\n"
|
|
|
|
" }\n"
|
|
|
|
" return s;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse8()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[10];\n"
|
|
|
|
" if ( s )\n"
|
|
|
|
" {\n"
|
|
|
|
" return s;\n"
|
|
|
|
" }\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ifelse9()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[10];\n"
|
|
|
|
" if ( ghfgf )\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] s;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-25 20:20:23 +01:00
|
|
|
void ifelse10()
|
|
|
|
{
|
|
|
|
check("static char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[10];\n"
|
|
|
|
" if ( ghfgf )\n"
|
|
|
|
" {\n"
|
|
|
|
" str[0] = s;\n"
|
|
|
|
" }\n"
|
|
|
|
" else\n"
|
|
|
|
" {\n"
|
|
|
|
" str[0] = s;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n", true);
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-02-25 20:20:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
void if1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: p\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" if (0 != s)\n"
|
|
|
|
" foo(s);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = malloc(256);\n"
|
|
|
|
" if (somecondition && !p)\n"
|
|
|
|
" return;\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if6()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *a = 0;\n"
|
|
|
|
" a = fopen(\"test.txt\", \"rw\");\n"
|
|
|
|
" if( a == 0 )\n"
|
|
|
|
" {\n"
|
|
|
|
" a = fopen(\"test.txt\", \"r\");\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" fclose( a );\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void if7()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f( bool b )\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a=0;\n"
|
|
|
|
" if( b )\n"
|
|
|
|
" {\n"
|
|
|
|
" a = new int[10];\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" if( b )\n"
|
|
|
|
" delete [] a;\n"
|
|
|
|
" else {}\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2008-12-22 15:42:54 +01:00
|
|
|
void if8()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f(int i)\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = malloc(50);\n"
|
|
|
|
" if (i == 1)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(c);\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" if (i == 2)\n"
|
|
|
|
" {\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" free(c);\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: c\n", errout.str());
|
2008-12-22 15:42:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-02 08:44:49 +01:00
|
|
|
void if9()
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f()\n"
|
|
|
|
"{\n"
|
2009-03-23 18:34:59 +01:00
|
|
|
" char *buf = NULL, *tmp;\n"
|
|
|
|
" if (!(tmp = realloc(buf, 50)))\n"
|
|
|
|
" {\n"
|
|
|
|
" free(buf);\n"
|
|
|
|
" return NULL;\n"
|
|
|
|
" }\n"
|
|
|
|
" buf = tmp;\n"
|
|
|
|
" return buf;\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-02 09:16:32 +01:00
|
|
|
void if10()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f()\n"
|
|
|
|
"{\n"
|
2009-03-23 18:34:59 +01:00
|
|
|
" char *buf = malloc(10);\n"
|
|
|
|
" if (aa)\n"
|
|
|
|
" ;\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
" else if (buf = realloc(buf, 100))\n"
|
2009-03-23 18:34:59 +01:00
|
|
|
" ;\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
" free(buf);\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: buf\n", errout.str());
|
2009-01-02 09:16:32 +01:00
|
|
|
}
|
|
|
|
|
2009-02-04 07:11:36 +01:00
|
|
|
void if11()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *x = new int[10];\n"
|
|
|
|
" if (x == 0 || aa)\n"
|
|
|
|
" {\n"
|
|
|
|
" return 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" delete [] x;\n"
|
|
|
|
"}\n", true);
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x\n", errout.str());
|
2009-02-04 07:11:36 +01:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-02 08:44:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-18 22:28:57 +01: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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void forwhile3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void forwhile4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void forwhile5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void forwhile6()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void forwhile8()
|
|
|
|
{
|
|
|
|
check("char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = 0;\n"
|
|
|
|
" int i = 0;\n"
|
|
|
|
" for( ;; )\n"
|
|
|
|
" {\n"
|
2009-01-01 23:22:28 +01:00
|
|
|
" ++i;\n"
|
2008-12-18 22:28:57 +01:00
|
|
|
" a = realloc( a, i );\n"
|
|
|
|
" if( !a )\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"\n"
|
|
|
|
" if( i > 10 )\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: a\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-10 17:28:04 +01:00
|
|
|
void forwhile9()
|
|
|
|
{
|
|
|
|
check("char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = 0;\n"
|
|
|
|
" int i = 0;\n"
|
2009-04-05 01:55:45 +02:00
|
|
|
" for(i = 0 ;i < 50 ; i++)\n"
|
2009-01-10 17:28:04 +01:00
|
|
|
" {\n"
|
2009-04-05 01:55:45 +02:00
|
|
|
" if(func1(i))\n"
|
|
|
|
" continue;\n"
|
|
|
|
" a = realloc( a, i );\n"
|
|
|
|
" if(func2(i))\n"
|
|
|
|
" continue;\n"
|
2009-01-10 17:28:04 +01:00
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}\n", true);
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-10 17:28:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void forwhile10()
|
|
|
|
{
|
|
|
|
check("char *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = 0;\n"
|
|
|
|
" int i = 0;\n"
|
2009-04-05 01:55:45 +02:00
|
|
|
" for(i = 0; i < 50; i++)\n"
|
2009-01-10 17:28:04 +01:00
|
|
|
" {\n"
|
2009-04-05 01:55:45 +02:00
|
|
|
" if(func1(i))\n"
|
|
|
|
" continue;\n"
|
|
|
|
" a = realloc( a, i );\n"
|
|
|
|
" if(func2(i))\n"
|
|
|
|
" return;\n"
|
2009-01-10 17:28:04 +01:00
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}\n", true);
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: a\n", errout.str());
|
2009-01-10 17:28:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 13:58:30 +02:00
|
|
|
void forwhile11()
|
|
|
|
{
|
|
|
|
check("int main()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *stream=NULL;\n"
|
|
|
|
" while((stream = fopen(name,\"r\")) == NULL)\n"
|
|
|
|
" { }\n"
|
|
|
|
" if(stream!=NULL) fclose(stream);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void dowhile1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = strdup(\"abc\");\n"
|
|
|
|
" do\n"
|
|
|
|
" {\n"
|
|
|
|
" str = strdup(\"def\");\n"
|
|
|
|
" }\n"
|
|
|
|
" while (!str);\n"
|
|
|
|
" return str;\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01: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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void switch2()
|
|
|
|
{
|
2009-01-07 18:49:21 +01:00
|
|
|
const std::string code("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");
|
|
|
|
check(code.c_str(), false);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
check(code.c_str(), true);
|
2009-02-09 21:16:00 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:12]: (all) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-17 08:55:40 +01:00
|
|
|
void switch3()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[10];\n"
|
|
|
|
" while (abc)\n"
|
|
|
|
" {\n"
|
|
|
|
" switch (def)\n"
|
|
|
|
" {\n"
|
|
|
|
" default:\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n");
|
2009-02-05 22:06:32 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: str\n", errout.str());
|
2009-01-17 08:55:40 +01:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ret1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("char *f( char **str )\n"
|
|
|
|
"{\n"
|
|
|
|
" char *ret = malloc( 10 );\n"
|
|
|
|
" return *str = ret;\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ret2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: abc\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ret3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *filep = fopen(\"myfile.txt\",\"w\");\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: filep\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ret4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *p = popen( \"ls -l\", \"r\");\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: p\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2008-12-25 09:27:07 +01:00
|
|
|
void ret5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static char * f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = new char[50];\n"
|
|
|
|
" return (c ? c : NULL);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-25 09:27:07 +01:00
|
|
|
}
|
|
|
|
|
2009-02-24 07:23:21 +01:00
|
|
|
void ret6()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = new char[50];\n"
|
|
|
|
" return strcpy(c, \"foo\");\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-02-24 07:23:21 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 21:39:17 +01:00
|
|
|
void ret7()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = new char[50];\n"
|
|
|
|
" return memcpy(c, \"foo\",4);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-03-03 21:39:17 +01:00
|
|
|
}
|
2008-12-25 09:27:07 +01:00
|
|
|
|
2009-06-05 08:56:46 +02:00
|
|
|
void ret8()
|
|
|
|
{
|
|
|
|
check("char *foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = new char[50];\n"
|
|
|
|
" return ((char *)(c+1));\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2008-12-25 09:27:07 +01:00
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
void mismatch1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a = new int[10];\n"
|
|
|
|
" free(a);\n"
|
2009-01-31 18:54:31 +01:00
|
|
|
"}\n", true);
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: a\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-31 12:51:47 +01:00
|
|
|
void mismatch2()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
2009-02-02 06:58:50 +01:00
|
|
|
" FILE *fp;\n"
|
2009-01-31 12:51:47 +01:00
|
|
|
"\n"
|
2009-02-02 06:58:50 +01:00
|
|
|
" fp = fopen();\n"
|
|
|
|
" fclose(fp);\n"
|
2009-01-31 12:51:47 +01:00
|
|
|
"\n"
|
2009-02-02 06:58:50 +01:00
|
|
|
" fp = popen();\n"
|
|
|
|
" pclose(fp);\n"
|
2009-01-31 18:54:31 +01:00
|
|
|
"}\n", false);
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-31 12:51:47 +01:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-02-02 06:58:50 +01:00
|
|
|
void mismatch3()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *fp;\n"
|
|
|
|
"\n"
|
|
|
|
" if (abc) fp = fopen();\n"
|
|
|
|
" else fp = popen();\n"
|
|
|
|
"\n"
|
|
|
|
" if (abc) fclose(fp);\n"
|
|
|
|
" else pclose(fp);\n"
|
|
|
|
"}\n", false);
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-02-02 06:58:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void mismatch4()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = 0;\n"
|
|
|
|
" for (i = 0; i < 10; ++i)\n"
|
|
|
|
" {\n"
|
|
|
|
" delete p;\n"
|
|
|
|
" p = new char[100];\n"
|
|
|
|
" }\n"
|
|
|
|
" delete [] p;\n"
|
|
|
|
"}\n", false);
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Mismatching allocation and deallocation: p\n", errout.str());
|
2009-02-02 06:58:50 +01:00
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
// function calls
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
void func1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = new char[100];\n"
|
|
|
|
" foo.add(p);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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"
|
2009-01-31 18:54:31 +01:00
|
|
|
"}\n", true);
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: str\n",
|
|
|
|
errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func6()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: p\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func7()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-14 07:33:46 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func8()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[100];"
|
|
|
|
" (*release)(str);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func9()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2008-12-25 08:50:25 +01:00
|
|
|
void func10()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f(void (*fnc)(char*))\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = malloc(50);\n"
|
|
|
|
" (fnc)(c);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-25 08:50:25 +01:00
|
|
|
}
|
|
|
|
|
2009-01-02 01:05:08 +01:00
|
|
|
void func11()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void f(struct1 *s1)\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = malloc(50);\n"
|
|
|
|
" (s1->fnc)(c);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-02 01:05:08 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 08:40:09 +01:00
|
|
|
void func12()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void add_list(struct mmtimer *n)\n"
|
|
|
|
"{\n"
|
|
|
|
" rb_link_node(&n->list, parent, link);\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"int foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct mmtimer *base;\n"
|
|
|
|
"\n"
|
|
|
|
" base = kmalloc(sizeof(struct mmtimer), GFP_KERNEL);\n"
|
|
|
|
" if (base == NULL)\n"
|
|
|
|
" return -ENOMEM;\n"
|
|
|
|
"\n"
|
|
|
|
" add_list(base);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-03 08:40:09 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 19:55:56 +01:00
|
|
|
void func13()
|
|
|
|
{
|
|
|
|
check("static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" foo(&p);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-24 19:55:56 +01:00
|
|
|
}
|
|
|
|
|
2009-06-12 17:31:29 +02:00
|
|
|
void func14()
|
|
|
|
{
|
|
|
|
// It is not known what the "foo" that only takes one parameter does..
|
|
|
|
check("static void foo(char *a, char *b)\n"
|
|
|
|
"{\n"
|
|
|
|
" free(a);\n"
|
|
|
|
" free(b);\n"
|
|
|
|
"}\n"
|
|
|
|
"static void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" foo(p);\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2009-01-24 19:55:56 +01:00
|
|
|
|
2009-06-16 19:05:05 +02:00
|
|
|
void func15()
|
|
|
|
{
|
|
|
|
check("static void a()\n"
|
|
|
|
"{ return true; }\n"
|
|
|
|
"\n"
|
|
|
|
"static void b()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" if (a()) return;\n" // <- memory leak
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-15 17:44:59 +02:00
|
|
|
void allocfunc1()
|
|
|
|
{
|
|
|
|
check("static char *a()\n"
|
|
|
|
"{\n"
|
|
|
|
" return new char[100];\n"
|
|
|
|
"}\n"
|
|
|
|
"static void b()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = a();\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p\n"), errout.str());
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void throw1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[10];\n"
|
|
|
|
" if ( ! abc )\n"
|
|
|
|
" throw 123;\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: str\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2008-12-27 12:29:38 +01:00
|
|
|
void throw2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = 0;\n"
|
|
|
|
" try\n"
|
|
|
|
" {\n"
|
|
|
|
" str = new char[100];\n"
|
|
|
|
" if ( somecondition )\n"
|
|
|
|
" throw exception;\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
" }\n"
|
|
|
|
" catch ( ... )\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
2008-12-27 12:29:38 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-27 12:29:38 +01:00
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void linux_list_1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sizeof1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: s2\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void realloc1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = realloc(a, 100);\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void realloc2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = (char *)realloc(a, 100);\n"
|
|
|
|
" free(a);\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-03-22 21:42:01 +01:00
|
|
|
void realloc3()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = 0;\n"
|
|
|
|
" if ((a = realloc(a, 100)) == NULL)\n"
|
|
|
|
" return;\n"
|
|
|
|
" free(a);\n"
|
|
|
|
"}\n");
|
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-03-22 21:42:01 +01:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:13:06 +01:00
|
|
|
void realloc4()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" static char *a = 0;\n"
|
|
|
|
" if ((a = realloc(a, 100)) == NULL)\n"
|
|
|
|
" return;\n"
|
|
|
|
" free(a);\n"
|
|
|
|
"}\n");
|
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n", errout.str());
|
2009-03-28 16:13:06 +01:00
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
void assign()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = 0;\n"
|
|
|
|
" free(a);\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" char *p = a;\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-26 21:12:42 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" char *p = a + 1;\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n");
|
2008-12-26 21:12:42 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a += 10;\n"
|
|
|
|
" free(a - 10);\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = (char *)malloc(10);\n"
|
|
|
|
" a = (void *)a + 10;\n"
|
|
|
|
" free(a - 10);\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-28 21:57:50 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = new char[100];\n"
|
|
|
|
" list += a;\n"
|
|
|
|
"}\n");
|
2008-12-28 21:57:50 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void varid()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" {\n"
|
|
|
|
" char *p = 0;\n"
|
|
|
|
" delete p;\n"
|
|
|
|
" }\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
TODO_ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cast1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = reinterpret_cast<char *>(malloc(10));\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cast2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = malloc(10);\n"
|
|
|
|
" free((void *)a);\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cast3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = malloc(10);\n"
|
|
|
|
" free(reinterpret_cast<void *>(a));\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void structmember1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct ABC *abc = new ABC;\n"
|
|
|
|
" abc->a = new char[100];\n"
|
|
|
|
" delete abc;\n"
|
|
|
|
"}\n");
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: abc.a\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2008-12-31 10:02:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void dealloc_use_1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *s = new char[100];\n"
|
|
|
|
" delete [] s;\n"
|
|
|
|
" p = s;\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Using 's' after it is deallocated / released\n", errout.str());
|
2008-12-31 10:02:45 +01:00
|
|
|
}
|
|
|
|
|
2009-01-01 09:15:27 +01:00
|
|
|
void dealloc_use_2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str;\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" foo(&str);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-01 09:15:27 +01:00
|
|
|
}
|
|
|
|
|
2009-01-01 11:14:52 +01:00
|
|
|
void dealloc_use_3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = 0;\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" f1(&str);\n"
|
|
|
|
" f2(str);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-01 11:14:52 +01:00
|
|
|
}
|
|
|
|
|
2009-01-01 12:02:30 +01:00
|
|
|
void dealloc_use_4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("static void ReadDir(DIR *d)\n"
|
|
|
|
"{\n"
|
|
|
|
" DIR *subdir = OpenDir();\n"
|
|
|
|
" ReadDir( subdir );\n"
|
|
|
|
" closedir(subdir);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-01 12:02:30 +01:00
|
|
|
}
|
2008-12-31 10:02:45 +01:00
|
|
|
|
2009-01-02 09:00:12 +01:00
|
|
|
void dealloc_use_5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
2009-03-25 07:10:17 +01:00
|
|
|
" char *str = malloc(10);\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
" free(str);\n"
|
|
|
|
" char c = str[10];\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Using 'str' after it is deallocated / released\n", errout.str());
|
2009-01-02 09:00:12 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 08:47:35 +01:00
|
|
|
void dealloc_use_6()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = 0;\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" printf(\"free %x\", str);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-03 08:47:35 +01:00
|
|
|
}
|
|
|
|
|
2009-02-05 21:17:01 +01:00
|
|
|
void dealloc_use_7()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[10];\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
" str[10] = 0;\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Using 'str' after it is deallocated / released\n", errout.str());
|
2009-02-05 21:17:01 +01:00
|
|
|
}
|
|
|
|
|
2009-01-15 21:34:39 +01:00
|
|
|
|
2009-01-16 17:29:41 +01:00
|
|
|
void freefree1()
|
2009-01-15 21:34:39 +01:00
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = malloc(100);\n"
|
|
|
|
" free(str);\n"
|
|
|
|
" free(str);\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Deallocating a deallocated pointer: str\n", errout.str());
|
2009-01-15 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-16 17:29:41 +01:00
|
|
|
void freefree2()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *fd = fopen(\"test.txt\", \"wb\");\n"
|
|
|
|
" fprintf(fd, \"test\");\n"
|
|
|
|
" fclose(fd);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-16 17:29:41 +01:00
|
|
|
}
|
|
|
|
|
2009-06-06 20:55:16 +02:00
|
|
|
void strcpy_result_assignment()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p1 = malloc(10);\n"
|
|
|
|
" char *p2 = strcpy(p1, \"a\");\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-01-25 16:56:19 +01:00
|
|
|
void strcat_result_assignment()
|
|
|
|
{
|
2009-02-07 10:44:57 +01:00
|
|
|
check("void foo()\n"
|
2009-01-25 16:56:19 +01:00
|
|
|
"{\n"
|
2009-02-07 10:44:57 +01:00
|
|
|
" char *p = malloc(10);\n"
|
|
|
|
" p[0] = 0;\n"
|
|
|
|
" p = strcat( p, \"a\" );\n"
|
|
|
|
" free( p );\n"
|
|
|
|
" return 0;\n"
|
2009-01-25 16:56:19 +01:00
|
|
|
"}");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-25 16:56:19 +01:00
|
|
|
}
|
2009-01-31 14:57:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void all1()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred *f = new Fred;\n"
|
|
|
|
"}\n", false);
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-31 14:57:27 +01:00
|
|
|
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred *f = new Fred;\n"
|
|
|
|
"}\n", true);
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: f\n", errout.str());
|
2009-01-31 14:57:27 +01:00
|
|
|
}
|
|
|
|
|
2009-02-07 11:54:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
void malloc_constant_1()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = malloc(3);\n"
|
|
|
|
" free(p);\n"
|
|
|
|
"}\n", false);
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) The given size 3 is mismatching\n", errout.str());
|
2009-02-07 11:54:39 +01:00
|
|
|
}
|
2009-02-08 09:21:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void unknownFunction1()
|
|
|
|
{
|
2009-02-08 12:59:04 +01:00
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new int[100];\n"
|
|
|
|
" if (abc)\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] p;\n"
|
|
|
|
" ThrowException();\n"
|
|
|
|
" }\n"
|
|
|
|
" delete [] p;\n"
|
|
|
|
"}\n");
|
2009-02-08 09:21:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-02-08 12:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void unknownFunction2()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new int[100];\n"
|
|
|
|
" if (abc)\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] p;\n"
|
|
|
|
" ThrowException();\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
2009-02-08 19:27:09 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: p\n", errout.str());
|
2009-06-03 22:20:33 +02:00
|
|
|
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new int[100];\n"
|
|
|
|
" p = g();\n"
|
|
|
|
" delete [] p;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
|
2009-02-08 09:21:15 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 19:31:09 +01:00
|
|
|
void unknownFunction3()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new int[100];\n"
|
2009-02-08 19:50:34 +01:00
|
|
|
" ThrowException();\n"
|
2009-02-08 19:31:09 +01:00
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-06-21 13:48:39 +02:00
|
|
|
void unknownFunction4()
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = new int[100];\n"
|
|
|
|
" a();\n"
|
|
|
|
" if (b) return;\n"
|
|
|
|
" delete [] p;\n"
|
2009-06-21 14:12:59 +02:00
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
|
2009-06-21 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
2009-06-23 18:29:43 +02:00
|
|
|
void unknownFunction5()
|
|
|
|
{
|
|
|
|
check("static void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *p = NULL;\n"
|
|
|
|
"\n"
|
|
|
|
" if( a )\n"
|
|
|
|
" p = malloc(100);\n"
|
|
|
|
"\n"
|
|
|
|
" if( a )\n"
|
|
|
|
" {\n"
|
|
|
|
" FREENULL(p);\n"
|
|
|
|
" FREENULL();\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2009-02-20 07:28:18 +01:00
|
|
|
|
|
|
|
|
2009-03-06 07:22:07 +01:00
|
|
|
void checkvcl(const char code[], const char _autoDealloc[])
|
2009-02-20 07:28:18 +01:00
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
2009-03-06 07:22:07 +01:00
|
|
|
{
|
2009-03-06 18:13:41 +01:00
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
2009-03-06 07:22:07 +01:00
|
|
|
}
|
2009-02-20 07:28:18 +01:00
|
|
|
tokenizer.setVarId();
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check for memory leaks..
|
|
|
|
Settings settings;
|
|
|
|
settings._debug = true;
|
|
|
|
settings._showAll = true;
|
2009-03-06 18:13:41 +01:00
|
|
|
|
2009-03-06 07:22:07 +01:00
|
|
|
{
|
2009-03-06 18:13:41 +01:00
|
|
|
std::istringstream istr(_autoDealloc);
|
|
|
|
settings.autoDealloc(istr);
|
2009-03-06 07:22:07 +01:00
|
|
|
}
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, this);
|
|
|
|
checkMemoryLeak.check();
|
2009-02-23 20:32:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-20 07:28:18 +01:00
|
|
|
|
2009-02-23 20:32:54 +01:00
|
|
|
void vcl1()
|
|
|
|
{
|
|
|
|
checkvcl("void Form1::foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" TEdit *Edit1 = new TEdit(this);\n"
|
2009-03-06 07:22:07 +01:00
|
|
|
"}\n", "TEdit\n");
|
2009-02-20 07:28:18 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:32:54 +01:00
|
|
|
|
|
|
|
void vcl2()
|
|
|
|
{
|
|
|
|
checkvcl("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" TButton *button;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" button = new TButton(this);\n"
|
2009-03-06 07:22:07 +01:00
|
|
|
"}\n", "TButton\n");
|
2009-02-23 20:32:54 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2009-04-28 20:01:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
void autoptr1()
|
|
|
|
{
|
|
|
|
check("std::auto_ptr<int> foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *i = new int;\n"
|
|
|
|
" return std::auto_ptr<int>(i);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
void if_with_and()
|
2009-04-29 20:40:21 +02:00
|
|
|
{
|
2009-06-08 20:20:43 +02:00
|
|
|
check("void f()\n"
|
2009-04-29 20:40:21 +02:00
|
|
|
"{\n"
|
2009-06-08 20:20:43 +02:00
|
|
|
" char *a = new char[10];\n"
|
|
|
|
" if (!a && b() )\n"
|
|
|
|
" return;\n"
|
2009-05-01 11:29:49 +02:00
|
|
|
"\n"
|
|
|
|
" delete [] a;\n"
|
|
|
|
"}\n", true);
|
2009-05-01 11:28:33 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2009-05-01 11:29:49 +02:00
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a = new char[10];\n"
|
|
|
|
" if (b() && !a )\n"
|
|
|
|
" return;\n"
|
|
|
|
"\n"
|
|
|
|
" delete [] a;\n"
|
|
|
|
"}\n", true);
|
2009-05-01 13:39:57 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-01 11:28:33 +02:00
|
|
|
}
|
2009-05-06 20:20:25 +02:00
|
|
|
|
|
|
|
void assign_pclose()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *f = popen (\"test\", \"w\");\n"
|
|
|
|
" int a = pclose(f);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-06 20:20:25 +02:00
|
|
|
}
|
2009-05-10 08:01:38 +02:00
|
|
|
|
|
|
|
void exit1()
|
|
|
|
{
|
|
|
|
// Ticket #297
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *out = new char[100];\n"
|
|
|
|
" if (c())\n"
|
|
|
|
" {\n"
|
|
|
|
" delete [] out;\n"
|
|
|
|
" exit(0);\n"
|
|
|
|
" }\n"
|
|
|
|
" delete [] out;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-10 08:01:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit2()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *out = new char[100];\n"
|
|
|
|
" exit(0);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-06-07 08:55:20 +02:00
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *out = new char[100];\n"
|
|
|
|
" if( out ) {}\n"
|
|
|
|
" exit(0);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-10 08:01:38 +02:00
|
|
|
}
|
2009-05-12 21:04:49 +02:00
|
|
|
|
|
|
|
void stdstring()
|
|
|
|
{
|
|
|
|
check("void f(std::string foo)\n"
|
|
|
|
"{\n"
|
|
|
|
" char *out = new char[11];\n"
|
|
|
|
" memset(&(out[0]), 0, 1);\n"
|
|
|
|
"}\n");
|
|
|
|
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: out\n", errout.str());
|
2009-05-12 21:04:49 +02:00
|
|
|
}
|
2009-05-19 22:29:10 +02:00
|
|
|
|
|
|
|
void strndup_function()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *out = strndup(\"text\", 3);\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: out\n", errout.str());
|
2009-05-19 22:29:10 +02:00
|
|
|
}
|
|
|
|
|
2009-06-01 12:40:24 +02:00
|
|
|
void tmpfile_function()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *f = tmpfile();\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
|
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *f = tmpfile();\n"
|
|
|
|
" if (!f)\n"
|
|
|
|
" return;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: f\n", errout.str());
|
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *f = tmpfile();\n"
|
|
|
|
" fclose(f);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *f = tmpfile();\n"
|
|
|
|
" if (!f)\n"
|
|
|
|
" return;\n"
|
|
|
|
" fclose(f);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("FILE *f()\n"
|
|
|
|
"{\n"
|
|
|
|
" return tmpfile();\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-05-22 16:47:40 +02:00
|
|
|
void fcloseall_function()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *f = fopen(fname, str);\n"
|
|
|
|
" fcloseall();\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-06-01 12:40:24 +02:00
|
|
|
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *f = tmpfile();\n"
|
|
|
|
" fcloseall();\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-22 16:47:40 +02:00
|
|
|
}
|
|
|
|
|
2009-06-21 17:01:43 +02:00
|
|
|
void open_function()
|
|
|
|
{
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = open(path, O_RDONLY);\n"
|
|
|
|
"}\n", true);
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd\n", errout.str());
|
|
|
|
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = open(path, O_RDONLY);\n"
|
|
|
|
" if (fd == -1)\n"
|
|
|
|
" return;\n"
|
|
|
|
" close(fd);\n"
|
|
|
|
"}\n", true);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = open(path, O_RDONLY);\n"
|
|
|
|
" if (fd < 0)\n"
|
|
|
|
" return;\n"
|
|
|
|
" close(fd);\n"
|
|
|
|
"}\n", true);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void creat_function()
|
|
|
|
{
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = creat(path, S_IRWXU);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_function()
|
|
|
|
{
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = open(path, O_RDONLY);\n"
|
|
|
|
" close(fd);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = creat(path, S_IRWXU);\n"
|
|
|
|
" close(fd);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = creat(path, S_IRWXU);\n"
|
|
|
|
" if (close(fd) < 0) {\n"
|
|
|
|
" perror(\"close\");\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void fd_functions()
|
|
|
|
{
|
|
|
|
check("void f(const char *path)\n"
|
|
|
|
"{\n"
|
|
|
|
" int fd = open(path, O_RDONLY);\n"
|
|
|
|
" read(fd, buf, count);\n"
|
|
|
|
" readv(fd, iov, iovcnt);\n"
|
|
|
|
" readahead(fd, offset, count);\n"
|
|
|
|
" pread(fd, buf, count, offset);\n"
|
|
|
|
" write(fd, buf, count);\n"
|
|
|
|
" writev(fd, iov, iovcnt);\n"
|
|
|
|
" pwrite(fd, buf, count, offset);\n"
|
|
|
|
" ioctl(fd, request);\n"
|
|
|
|
" posix_fallocate(fd, offset, len);\n"
|
|
|
|
" posix_fadvise(fd, offset, len, advise);\n"
|
|
|
|
" fsync(fd);\n"
|
|
|
|
" fdatasync(fd);\n"
|
|
|
|
" sync_file_range(fd, offset, nbytes, flags);\n"
|
|
|
|
" lseek(fd, offset, whence);\n"
|
|
|
|
" fcntl(fd, cmd);\n"
|
|
|
|
" flock(fd, op);\n"
|
|
|
|
" lockf(fd, cmd, len);\n"
|
|
|
|
" ftruncate(fd, len);\n"
|
|
|
|
" fstat(fd, buf);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:23]: (error) Resource leak: fd\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-05-22 09:24:03 +02:00
|
|
|
void opendir_function()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" DIR *f = opendir(\".\");\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
|
2009-05-22 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void fdopendir_function()
|
|
|
|
{
|
|
|
|
check("void f(int fd)\n"
|
|
|
|
"{\n"
|
|
|
|
" DIR *f = fdopendir(fd);\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
|
2009-05-22 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void closedir_function()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" DIR *f = opendir(\".\");\n"
|
|
|
|
" closedir(f);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-22 09:24:03 +02:00
|
|
|
|
|
|
|
check("void f(int fd)\n"
|
|
|
|
"{\n"
|
|
|
|
" DIR *f = fdopendir(fd);\n"
|
|
|
|
" closedir(f);\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-22 09:24:03 +02:00
|
|
|
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" DIR * f = opendir(dirname);\n"
|
|
|
|
" if (closedir(f));\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-22 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void dir_functions()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" DIR *f = opendir(dir);\n"
|
|
|
|
" readdir(f);\n;"
|
|
|
|
" readdir_r(f, entry, res);\n;"
|
|
|
|
" rewinddir(f);\n;"
|
|
|
|
" telldir(f);\n;"
|
|
|
|
" seekdir(f, 2)\n;"
|
|
|
|
" scandir(f, namelist, filter, comp);\n;"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:10]: (error) Resource leak: f\n", errout.str());
|
2009-05-22 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
2009-05-23 13:01:18 +02:00
|
|
|
void file_functions()
|
2009-05-23 12:20:26 +02:00
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
"FILE *f = fopen(fname, str);\n"
|
|
|
|
"feof(f);\n"
|
2009-05-23 13:01:18 +02:00
|
|
|
"clearerr(in);\n"
|
|
|
|
"ferror(in);\n"
|
|
|
|
"fread(ptr, 10, 1, in);\n"
|
|
|
|
"fwrite(ptr, 10, 1, in);\n"
|
|
|
|
"fflush(in);\n"
|
|
|
|
"setbuf(in, buf);\n"
|
|
|
|
"setbuffer(in, buf, 100);\n"
|
|
|
|
"setlinebuf(in);\n"
|
|
|
|
"setvbuf(in, buf, _IOLBF, 0);\n"
|
|
|
|
"fseek(in, 10, SEEK_SET);\n"
|
|
|
|
"fseeko(in, 10, SEEK_SET);\n"
|
|
|
|
"ftell(in);\n"
|
|
|
|
"ftello(in);\n"
|
|
|
|
"rewind(in);\n"
|
|
|
|
"fsetpos(in, 0);\n"
|
|
|
|
"fgetpos(in, 10);\n"
|
|
|
|
"fprintf(in, \"text\\n\");\n"
|
|
|
|
"}\n");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:22]: (error) Resource leak: f\n", errout.str());
|
2009-05-23 12:20:26 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:36:03 +02:00
|
|
|
void pointer_to_pointer()
|
|
|
|
{
|
|
|
|
check("void f(char **data)\n"
|
|
|
|
"{\n"
|
|
|
|
" char *c = new char[12];\n"
|
|
|
|
" *c = 0;\n"
|
|
|
|
" *data = c;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-22 22:36:03 +02:00
|
|
|
}
|
2009-05-27 21:16:54 +02:00
|
|
|
|
|
|
|
void dealloc_and_alloc_in_func()
|
|
|
|
{
|
|
|
|
check("char *f( const char *x )\n"
|
|
|
|
"{\n"
|
|
|
|
" delete [] x;\n"
|
|
|
|
" return new char[10];\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"int main()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *a=0;\n"
|
|
|
|
" a = f( a );\n"
|
|
|
|
" a[0] = 1;\n"
|
|
|
|
" delete [] a;\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-27 21:16:54 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
static TestMemleakInFunction testMemleakInFunction;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMemleakInClass : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestMemleakInClass() : TestFixture("TestMemleakInClass")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void check(const char code[], bool showAll = false)
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
tokenizer.setVarId();
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check for memory leaks..
|
|
|
|
Settings settings;
|
|
|
|
settings._debug = true;
|
|
|
|
settings._showAll = showAll;
|
|
|
|
tokenizer.fillFunctionList();
|
|
|
|
CheckMemoryLeakInClass checkMemoryLeak(&tokenizer, &settings, this);
|
|
|
|
checkMemoryLeak.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
TEST_CASE(class1);
|
|
|
|
TEST_CASE(class2);
|
|
|
|
TEST_CASE(class3);
|
|
|
|
TEST_CASE(class4);
|
|
|
|
TEST_CASE(class6);
|
|
|
|
TEST_CASE(class7);
|
|
|
|
TEST_CASE(class8);
|
|
|
|
TEST_CASE(class9);
|
|
|
|
TEST_CASE(class10);
|
|
|
|
TEST_CASE(class11);
|
|
|
|
|
|
|
|
TEST_CASE(free_member_in_sub_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
"}\n", true);
|
|
|
|
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (all) 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"
|
|
|
|
"}\n", true);
|
|
|
|
|
|
|
|
ASSERT_EQUALS("[test.cpp:17]: (error) Mismatching allocation and deallocation: Fred::str1\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class3()
|
|
|
|
{
|
|
|
|
check("class Token;\n"
|
|
|
|
"\n"
|
|
|
|
"class Tokenizer\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" Token *_tokens;\n"
|
|
|
|
"\n"
|
|
|
|
"public:\n"
|
|
|
|
" Tokenizer();\n"
|
|
|
|
" ~Tokenizer();\n"
|
|
|
|
" void deleteTokens(Token *tok);\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"Tokenizer::Tokenizer()\n"
|
|
|
|
"{\n"
|
|
|
|
" _tokens = new Token;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"Tokenizer::~Tokenizer()\n"
|
|
|
|
"{\n"
|
|
|
|
" deleteTokens(_tokens);\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void Tokenizer::deleteTokens(Token *tok)\n"
|
|
|
|
"{\n"
|
|
|
|
" while (tok)\n"
|
|
|
|
" {\n"
|
|
|
|
" Token *next = tok->next();\n"
|
|
|
|
" delete tok;\n"
|
|
|
|
" tok = next;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n", true);
|
|
|
|
|
|
|
|
TODO_ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class4()
|
|
|
|
{
|
|
|
|
check("struct ABC;\n"
|
|
|
|
"class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" void addAbc(ABC *abc);\n"
|
|
|
|
"public:\n"
|
|
|
|
" void click();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void Fred::addAbc(ABC* abc)\n"
|
|
|
|
"{\n"
|
|
|
|
" AbcPosts->Add(abc);\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void Fred::click()\n"
|
|
|
|
"{\n"
|
|
|
|
" ABC *p = new ABC;\n"
|
|
|
|
" addAbc( p );\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class6()
|
|
|
|
{
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" void foo();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void Fred::foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *str = new char[100];\n"
|
|
|
|
" delete [] str;\n"
|
|
|
|
" hello();\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class7()
|
|
|
|
{
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" int *i;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" ~Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" this->i = new int;\n"
|
|
|
|
"}\n"
|
|
|
|
"Fred::~Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" delete this->i;\n"
|
|
|
|
"}\n", true);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class8()
|
|
|
|
{
|
|
|
|
check("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" void a();\n"
|
|
|
|
" void doNothing() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void A::a()\n"
|
|
|
|
"{\n"
|
|
|
|
" int* c = new int(1);\n"
|
|
|
|
" delete c;\n"
|
|
|
|
" doNothing(c);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class9()
|
|
|
|
{
|
|
|
|
check("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" int * p;\n"
|
|
|
|
" A();\n"
|
|
|
|
" ~A();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"A::A()\n"
|
|
|
|
"{ p = new int; }\n"
|
|
|
|
"\n"
|
|
|
|
"A::~A()\n"
|
|
|
|
"{ delete (p); }\n", true);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class10()
|
|
|
|
{
|
|
|
|
check("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" int * p;\n"
|
|
|
|
" A() { p = new int; }\n"
|
|
|
|
"};\n", true);
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: A::p\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void class11()
|
|
|
|
{
|
|
|
|
check("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" int * p;\n"
|
|
|
|
" A();\n"
|
|
|
|
"};\n"
|
|
|
|
"A::A() : p(new int[10])\n"
|
|
|
|
"{ }", true);
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: A::p\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void free_member_in_sub_func()
|
|
|
|
{
|
2009-07-05 21:37:53 +02:00
|
|
|
// Member function
|
2009-06-08 20:20:43 +02:00
|
|
|
check("class Tokenizer\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Tokenizer();\n"
|
|
|
|
" ~Tokenizer();\n"
|
|
|
|
"\n"
|
|
|
|
"private:\n"
|
|
|
|
" int *_tokens;\n"
|
|
|
|
" static void deleteTokens(int *tok);\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"Tokenizer::Tokenizer()\n"
|
|
|
|
"{\n"
|
|
|
|
" _tokens = new int;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"Tokenizer::~Tokenizer()\n"
|
|
|
|
"{\n"
|
|
|
|
" deleteTokens(_tokens);\n"
|
|
|
|
" _tokens = 0;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void Tokenizer::deleteTokens(int *tok)\n"
|
|
|
|
"{\n"
|
|
|
|
" delete tok;\n"
|
|
|
|
"}\n", true);
|
|
|
|
TODO_ASSERT_EQUALS("", errout.str());
|
2009-07-05 21:37:53 +02:00
|
|
|
|
|
|
|
// Global function
|
|
|
|
check("void deleteTokens(int *tok)\n"
|
|
|
|
"{\n"
|
|
|
|
" delete tok;\n"
|
|
|
|
"}\n"
|
|
|
|
"class Tokenizer\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Tokenizer();\n"
|
|
|
|
" ~Tokenizer();\n"
|
|
|
|
"\n"
|
|
|
|
"private:\n"
|
|
|
|
" int *_tokens;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"Tokenizer::Tokenizer()\n"
|
|
|
|
"{\n"
|
|
|
|
" _tokens = new int;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"Tokenizer::~Tokenizer()\n"
|
|
|
|
"{\n"
|
|
|
|
" deleteTokens(_tokens);\n"
|
|
|
|
" _tokens = 0;\n"
|
|
|
|
"}\n", true);
|
|
|
|
TODO_ASSERT_EQUALS("", errout.str());
|
2009-06-08 20:20:43 +02:00
|
|
|
}
|
|
|
|
};
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-08 20:20:43 +02:00
|
|
|
static TestMemleakInClass testMemleakInClass;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-06-16 22:01:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMemleakStructMember : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestMemleakStructMember() : TestFixture("TestMemleakStructMember")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void check(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check for memory leaks..
|
|
|
|
Settings settings;
|
|
|
|
CheckMemoryLeakStructMember checkMemoryLeakStructMember(&tokenizer, &settings, this);
|
|
|
|
checkMemoryLeakStructMember.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
TEST_CASE(test1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test1()
|
|
|
|
{
|
|
|
|
check("static void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct ABC abc;\n"
|
|
|
|
" abc.a = malloc(10);\n"
|
|
|
|
"}\n");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: abc.a\n", errout.str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static TestMemleakStructMember testMemleakStructMember;
|
|
|
|
|