"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "test/testleakautovar.cpp" between
cppcheck-1.86.tar.gz and cppcheck-1.87.tar.gz

About: Cppcheck is a static analysis tool for C/C++ code. It checks for memory leaks, mismatching allocation-deallocation, buffer overrun, and many more.

testleakautovar.cpp  (cppcheck-1.86):testleakautovar.cpp  (cppcheck-1.87)
/* /*
* Cppcheck - A tool for static C/C++ code analysis * Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2018 Cppcheck team. * Copyright (C) 2007-2019 Cppcheck team.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
skipping to change at line 33 skipping to change at line 33
#include "tokenize.h" #include "tokenize.h"
class TestLeakAutoVar : public TestFixture { class TestLeakAutoVar : public TestFixture {
public: public:
TestLeakAutoVar() : TestFixture("TestLeakAutoVar") { TestLeakAutoVar() : TestFixture("TestLeakAutoVar") {
} }
private: private:
Settings settings; Settings settings;
void run() override { void run() OVERRIDE {
int id = 0; int id = 0;
while (!settings.library.ismemory(++id)); while (!settings.library.ismemory(++id));
settings.library.setalloc("malloc", id, -1); settings.library.setalloc("malloc", id, -1);
settings.library.setdealloc("free", id, 1); settings.library.setdealloc("free", id, 1);
while (!settings.library.isresource(++id)); while (!settings.library.isresource(++id));
settings.library.setalloc("fopen", id, -1); settings.library.setalloc("fopen", id, -1);
settings.library.setdealloc("fclose", id, 1); settings.library.setdealloc("fclose", id, 1);
// Assign // Assign
TEST_CASE(assign1); TEST_CASE(assign1);
skipping to change at line 99 skipping to change at line 99
TEST_CASE(ifelse1); TEST_CASE(ifelse1);
TEST_CASE(ifelse2); TEST_CASE(ifelse2);
TEST_CASE(ifelse3); TEST_CASE(ifelse3);
TEST_CASE(ifelse4); TEST_CASE(ifelse4);
TEST_CASE(ifelse5); TEST_CASE(ifelse5);
TEST_CASE(ifelse6); // #3370 TEST_CASE(ifelse6); // #3370
TEST_CASE(ifelse7); // #5576 - if (fd < 0) TEST_CASE(ifelse7); // #5576 - if (fd < 0)
TEST_CASE(ifelse8); // #5747 - if (fd == -1) TEST_CASE(ifelse8); // #5747 - if (fd == -1)
TEST_CASE(ifelse9); // #5273 - if (X(p==NULL, 0)) TEST_CASE(ifelse9); // #5273 - if (X(p==NULL, 0))
TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL)) TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL))
TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4)))
TEST_CASE(ifelse12); // #8340 - if ((*p = malloc(4)) == NULL)
TEST_CASE(ifelse13); // #8392
// switch // switch
TEST_CASE(switch1); TEST_CASE(switch1);
// loops // loops
TEST_CASE(loop1); TEST_CASE(loop1);
// mismatching allocation/deallocation // mismatching allocation/deallocation
TEST_CASE(mismatchAllocDealloc); TEST_CASE(mismatchAllocDealloc);
skipping to change at line 1161 skipping to change at line 1164
void ifelse10() { // #8794 void ifelse10() { // #8794
check("void f() {\n" check("void f() {\n"
" void *x = malloc(1U);\n" " void *x = malloc(1U);\n"
" if (!(x != NULL))\n" " if (!(x != NULL))\n"
" return;\n" " return;\n"
" free(x);\n" " free(x);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void ifelse11() { // #8365
check("void f() {\n"
" void *p;\n"
" if (NULL == (p = malloc(4)))\n"
" return;\n"
" free(p);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void ifelse12() { // #8340
check("void f(char **p) {\n"
" if ((*p = malloc(4)) == NULL)\n"
" return;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void ifelse13() { // #8392
check("int f(int fd, const char *mode) {\n"
" char *path;\n"
" if (fd == -1 || (path = (char *)malloc(10)) == NULL)\n"
" return 1;\n"
" free(path);\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f(int fd, const char *mode) {\n"
" char *path;\n"
" if ((path = (char *)malloc(10)) == NULL || fd == -1)\n"
" return 1;\n" // <- memory leak
" free(path);\n"
" return 0;\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:4] memory leak", "", errout.str());
}
void switch1() { void switch1() {
check("void f() {\n" check("void f() {\n"
" char *p = 0;\n" " char *p = 0;\n"
" switch (x) {\n" " switch (x) {\n"
" case 123: p = malloc(100); break;\n" " case 123: p = malloc(100); break;\n"
" default: return;\n" " default: return;\n"
" }\n" " }\n"
" free(p);\n" " free(p);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
skipping to change at line 1611 skipping to change at line 1652
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.c"); tokenizer.tokenize(istr, "test.c");
tokenizer.simplifyTokenList2(); tokenizer.simplifyTokenList2();
// Check for leaks.. // Check for leaks..
CheckLeakAutoVar checkLeak; CheckLeakAutoVar checkLeak;
checkLeak.runSimplifiedChecks(&tokenizer, &settings, this); checkLeak.runSimplifiedChecks(&tokenizer, &settings, this);
} }
void run() override { void run() OVERRIDE {
LOAD_LIB_2(settings.library, "windows.cfg"); LOAD_LIB_2(settings.library, "windows.cfg");
TEST_CASE(heapDoubleFree); TEST_CASE(heapDoubleFree);
} }
void heapDoubleFree() { void heapDoubleFree() {
check("void f() {" check("void f() {"
" HANDLE MyHeap = HeapCreate(0, 0, 0);" " HANDLE MyHeap = HeapCreate(0, 0, 0);"
" int *a = HeapAlloc(MyHeap, 0, sizeof(int));" " int *a = HeapAlloc(MyHeap, 0, sizeof(int));"
" int *b = HeapAlloc(MyHeap, 0, sizeof(int));" " int *b = HeapAlloc(MyHeap, 0, sizeof(int));"
 End of changes. 5 change blocks. 
3 lines changed or deleted 44 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)