X Tutup
Skip to content

Commit fe8ba51

Browse files
committed
TestMemLeak: moved posix.cfg tests
1 parent adedb5a commit fe8ba51

File tree

3 files changed

+66
-127
lines changed

3 files changed

+66
-127
lines changed

cfg/posix.cfg

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@
264264
<not-null/>
265265
</arg>
266266
</function>
267+
<function name="fdopendir">
268+
<noreturn>false</noreturn>
269+
<arg nr="1">
270+
<not-uninit/>
271+
</arg>
272+
</function>
267273
<function name="isatty">
268274
<noreturn>false</noreturn>
269275
<arg nr="1">
@@ -289,6 +295,19 @@
289295
<not-uninit/>
290296
</arg>
291297
</function>
298+
<!-- int socket(int domain, int type, int protocol); -->
299+
<function name="socket">
300+
<noreturn>false</noreturn>
301+
<arg nr="1">
302+
<not-uninit/>
303+
</arg>
304+
<arg nr="2">
305+
<not-uninit/>
306+
</arg>
307+
<arg nr="3">
308+
<not-uninit/>
309+
</arg>
310+
</function>
292311
<!-- int close(int fildes); -->
293312
<function name="close">
294313
<noreturn>false</noreturn>

test/cfg/posix.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
88
//
99

10+
#include <stdio.h> // <- FILE
11+
#include <dirent.h>
12+
#include <sys/mman.h>
13+
#include <sys/socket.h>
14+
#include <fcntl.h>
1015
#include <unistd.h>
1116

12-
void bufferAccessOutOf(int fd) {
17+
18+
void bufferAccessOutOfBounds(int fd) {
1319
char a[5];
1420
read(fd,a,5);
1521
// cppcheck-suppress bufferAccessOutOfBounds
@@ -32,11 +38,49 @@ void bufferAccessOutOf(int fd) {
3238
0;
3339
}
3440

35-
void f(char *p) {
41+
void nullPointer(char *p) {
3642
isatty (0);
3743
mkdir (p, 0);
3844
getcwd (0, 0);
3945
// cppcheck-suppress nullPointer
4046
readdir (0);
4147
}
4248

49+
void memleak_mmap(int fd) {
50+
void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
51+
// cppcheck-suppress memleak
52+
}
53+
54+
/* TODO: add configuration for fdopen
55+
void resourceLeak_fdopen(int fd) {
56+
FILE *f = fdopen(fd, "r");
57+
// cppcheck-suppress resourceLeak
58+
}
59+
*/
60+
61+
void resourceLeak_fdopendir(int fd) {
62+
DIR* leak1 = fdopendir(fd);
63+
// cppcheck-suppress resourceLeak
64+
}
65+
66+
void resourceLeak_opendir(void) {
67+
DIR* leak1 = opendir("abc");
68+
// cppcheck-suppress resourceLeak
69+
}
70+
71+
void resourceLeak_socket(void) {
72+
int s = socket(AF_INET, SOCK_STREAM, 0);
73+
// cppcheck-suppress resourceLeak
74+
}
75+
76+
void noleak(int x, int y, int z) {
77+
DIR *p1 = fdopendir(x); closedir(p1);
78+
DIR *p2 = opendir("abc"); closedir(p2);
79+
int s = socket(AF_INET,SOCK_STREAM,0); close(s);
80+
/* TODO: add configuration for open/fdopen
81+
// #2830
82+
int fd = open("path", O_RDONLY);
83+
FILE *f = fdopen(fd, "rt");
84+
fclose(f);
85+
*/
86+
}

test/testmemleak.cpp

Lines changed: 1 addition & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ class TestMemleak : private TestFixture {
100100
errout.str("");
101101

102102
Settings settings;
103-
settings.standards.posix = true;
104103

105104
Tokenizer tokenizer(&settings, this);
106105
std::istringstream istr(code);
@@ -330,7 +329,6 @@ class TestMemleakInFunction : public TestFixture {
330329
TEST_CASE(tmpfile_function);
331330
TEST_CASE(fcloseall_function);
332331
TEST_CASE(file_functions);
333-
TEST_CASE(posix_rewinddir);
334332
TEST_CASE(getc_function);
335333

336334
TEST_CASE(open_function);
@@ -366,10 +364,6 @@ class TestMemleakInFunction : public TestFixture {
366364

367365
TEST_CASE(c_code);
368366

369-
// test that the cfg files are configured correctly
370-
TEST_CASE(posixcfg);
371-
TEST_CASE(posixcfg_mmap);
372-
373367
TEST_CASE(gnucfg);
374368
}
375369

@@ -3700,22 +3694,13 @@ class TestMemleakInFunction : public TestFixture {
37003694
Settings settings;
37013695
settings.standards.posix = true;
37023696

3703-
check("void f()\n"
3704-
"{\n"
3697+
check("void f() {\n"
37053698
" FILE *f = popen (\"test\", \"w\");\n"
37063699
" int a = pclose(f);\n"
37073700
"}", &settings);
37083701
ASSERT_EQUALS("", errout.str());
37093702
}
37103703

3711-
void posix_rewinddir() {
3712-
Settings settings;
3713-
settings.standards.posix = true;
3714-
3715-
check("void f(DIR *p) { rewinddir(p); }", &settings);
3716-
ASSERT_EQUALS("", errout.str());
3717-
}
3718-
37193704
void exit2() {
37203705
check("void f()\n"
37213706
"{\n"
@@ -4280,114 +4265,6 @@ class TestMemleakInFunction : public TestFixture {
42804265
check(code, &settings);
42814266
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout.str());
42824267
}
4283-
4284-
// Test that posix.cfg is configured correctly
4285-
void posixcfg() {
4286-
Settings settings;
4287-
settings.standards.posix = true;
4288-
LOAD_LIB_2(settings.library, "posix.cfg");
4289-
4290-
const char code[] = "void leaks() {\n"
4291-
" void* leak1 = fdopendir();\n"
4292-
" void* leak2 = opendir();\n"
4293-
" void* leak3 = socket();\n"
4294-
"}\n"
4295-
"void noleaks() {\n"
4296-
" void *p1 = fdopendir(); closedir(p1);\n"
4297-
" void *p2 = opendir(); closedir(p2);\n"
4298-
" void *p3 = socket(); close(p3);\n"
4299-
"}";
4300-
check(code, &settings);
4301-
ASSERT_EQUALS("[test.cpp:5]: (error) Resource leak: leak1\n"
4302-
"[test.cpp:5]: (error) Resource leak: leak2\n"
4303-
"[test.cpp:5]: (error) Resource leak: leak3\n", errout.str());
4304-
4305-
const char code2[] = "int main() {\n"
4306-
" int fileDescriptor = socket(AF_INET, SOCK_STREAM, 0);\n"
4307-
" close(fileDescriptor);\n"
4308-
"}";
4309-
check(code2, &settings);
4310-
ASSERT_EQUALS("", errout.str());
4311-
4312-
// Ticket #2830
4313-
check("void f(const char *path) {\n"
4314-
" int fd = open(path, O_RDONLY);\n"
4315-
" FILE *f = fdopen(fd, x);\n"
4316-
" fclose(f);\n"
4317-
"}", &settings);
4318-
ASSERT_EQUALS("", errout.str());
4319-
4320-
// Ticket #1416
4321-
check("void f(void) {\n"
4322-
" FILE *f = fdopen(0, \"r\");\n"
4323-
"}", &settings);
4324-
ASSERT_EQUALS("[test.cpp:3]: (error) Resource leak: f\n", errout.str());
4325-
4326-
// strdupa allocates on the stack, no free() needed
4327-
check("void x()\n"
4328-
"{\n"
4329-
" char *s = strdupa(\"Test\");\n"
4330-
"}", &settings);
4331-
ASSERT_EQUALS("", errout.str());
4332-
4333-
LOAD_LIB_2(settings.library, "gtk.cfg");
4334-
4335-
check("void f(char *a) {\n"
4336-
" char *s = g_strdup(a);\n"
4337-
" mkstemp(s);\n"
4338-
" mkdtemp(s);\n"
4339-
" mktemp(s);\n"
4340-
"}", &settings);
4341-
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: s\n", errout.str());
4342-
}
4343-
4344-
void posixcfg_mmap() {
4345-
Settings settings;
4346-
settings.standards.posix = true;
4347-
LOAD_LIB_2(settings.library, "posix.cfg");
4348-
4349-
// normal mmap
4350-
check("void f(int fd) {\n"
4351-
" char *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n"
4352-
" munmap(addr, 255);\n"
4353-
"}", &settings);
4354-
ASSERT_EQUALS("", errout.str());
4355-
4356-
// mmap64 - large file support
4357-
check("void f(int fd) {\n"
4358-
" char *addr = mmap64(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n"
4359-
" munmap(addr, 255);\n"
4360-
"}", &settings);
4361-
ASSERT_EQUALS("", errout.str());
4362-
4363-
// pass in fixed address
4364-
check("void f(int fd) {\n"
4365-
" void *fixed_addr = 123;\n"
4366-
" void *mapped_addr = mmap(fixed_addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n"
4367-
" munmap(mapped_addr, 255);\n"
4368-
"}", &settings);
4369-
ASSERT_EQUALS("", errout.str());
4370-
4371-
// no munmap()
4372-
check("void f(int fd) {\n"
4373-
" void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n"
4374-
"}", &settings);
4375-
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: addr\n", errout.str());
4376-
4377-
// wrong deallocator
4378-
check("void f(int fd) {\n"
4379-
" void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n"
4380-
" free(addr);\n"
4381-
"}", &settings);
4382-
ASSERT_EQUALS("[test.cpp:3]: (error) Mismatching allocation and deallocation: addr\n", errout.str());
4383-
4384-
// wrong deallocator for mmap64
4385-
check("void f(int fd) {\n"
4386-
" void *addr = mmap64(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n"
4387-
" free(addr);\n"
4388-
"}", &settings);
4389-
ASSERT_EQUALS("[test.cpp:3]: (error) Mismatching allocation and deallocation: addr\n", errout.str());
4390-
}
43914268
};
43924269

43934270
static TestMemleakInFunction testMemleakInFunction;
@@ -6313,7 +6190,6 @@ class TestMemleakNoVar : public TestFixture {
63136190
}
63146191

63156192
void run() {
6316-
settings.standards.posix = true;
63176193
settings.inconclusive = true;
63186194
settings.addEnabled("warning");
63196195

0 commit comments

Comments
 (0)
X Tutup