Debian Codegraph @

tests/016_memleakcheck.c

raw symbols SCIP
1 /*
2 *
3 * check memory leak
4 *
5 */
6
7 #define _GNU_SOURCE
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <stdint.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <dlfcn.h>
15 #include <dirent.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <sys/signal.h>
20 #include <sys/mman.h>
21 #include <mcheck.h>
22
23 int dump_procmap() {
24 FILE *f;
25 int c;
26 char *p;
27
28 if (0 > asprintf(&p, "/proc/%jd/maps", (intmax_t)getpid())) {
29 perror("asprintf");
30 return 1;
31 }
32
33 if (!(f = fopen(p, "r"))) {
34 perror("fopen");
35 free(p);
36 return 1;
37 }
38 while (EOF != (c = fgetc(f))) {
39 fputc(c, stdout);
40 }
41 fclose(f);
42 free(p);
43 return 0;
44 }
45
46 int main(int argc, char **argv) {
47 int fd;
48 int i;
49 FILE *f;
50
51 /* Initialize */
52 if (-1 == (fd = open("1/a", O_WRONLY))) {
53 perror("open");
54 return 1;
55 }
56
57 if (5 != write(fd, "test\n", 5)) {
58 perror("write");
59 return 1;
60 }
61 if (-1 == close(fd)) {
62 perror("close");
63 return 1;
64 }
65
66 /* try open/close 100 times. */
67 mtrace();
68
69 for (i = 0; i < 100; ++i) {
70 if (-1 == (fd = open("1/b", O_WRONLY))) {
71 perror("open");
72 return 1;
73 }
74
75 if (5 != write(fd, "test\n", 5)) {
76 perror("write");
77 return 1;
78 }
79 if (-1 == close(fd)) {
80 perror("close");
81 return 1;
82 }
83
84 if (!(f = fopen("1/c", "w"))) {
85 return 1;
86 }
87
88 if (5 != fwrite("test\n", 1, 5, f)) {
89 return 1;
90 }
91 if (EOF == fclose(f)) {
92 return 1;
93 }
94 }
95
96 muntrace();
97
98 return dump_procmap();
99 }