forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
posix.c
239 lines (211 loc) · 5.66 KB
/
posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Test library configuration for posix.cfg
//
// Usage:
// $ cppcheck --check-library --library=posix --enable=information --error-exitcode=1 --inline-suppr --suppress=missingIncludeSystem test/cfg/posix.c
// =>
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
//
#include <stdlib.h>
#include <stdio.h> // <- FILE
#include <dirent.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <fcntl.h>
// unavailable on some linux systems #include <ndbm.h>
#include <netdb.h>
#include <regex.h>
#include <time.h>
#include <unistd.h>
void bufferAccessOutOfBounds(int fd)
{
char a[5];
read(fd,a,5);
// cppcheck-suppress bufferAccessOutOfBounds
read(fd,a,6);
write(fd,a,5);
// cppcheck-suppress bufferAccessOutOfBounds
write(fd,a,6);
recv(fd,a,5,0);
// cppcheck-suppress bufferAccessOutOfBounds
recv(fd,a,6,0);
recvfrom(fd,a,5,0,0x0,0x0);
// cppcheck-suppress bufferAccessOutOfBounds
recvfrom(fd,a,6,0,0x0,0x0);
send(fd,a,5,0);
// cppcheck-suppress bufferAccessOutOfBounds
send(fd,a,6,0);
sendto(fd,a,5,0,0x0,0x0);
// cppcheck-suppress bufferAccessOutOfBounds
sendto(fd,a,6,0,0x0,0x0);
// cppcheck-suppress constStatement
0;
}
void nullPointer(char *p)
{
// cppcheck-suppress ignoredReturnValue
isatty(0);
mkdir(p, 0);
getcwd(0, 0);
// cppcheck-suppress nullPointer
// cppcheck-suppress readdirCalled
readdir(0);
// cppcheck-suppress nullPointer
// cppcheck-suppress utimeCalled
utime(NULL, NULL);
}
void memleak_getaddrinfo()
{
//TODO: nothing to report yet, see http://sourceforge.net/p/cppcheck/discussion/general/thread/d9737d5d/
struct addrinfo * res=NULL;
getaddrinfo("node", NULL, NULL, &res);
freeaddrinfo(res);
}
void memleak_mmap(int fd)
{
// cppcheck-suppress unreadVariable
void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
// cppcheck-suppress memleak
}
/* TODO: add configuration for fdopen
void resourceLeak_fdopen(int fd) {
FILE *f = fdopen(fd, "r");
// cppcheck-suppress resourceLeak
}
*/
void resourceLeak_fdopendir(int fd)
{
// cppcheck-suppress unreadVariable
DIR* leak1 = fdopendir(fd);
// cppcheck-suppress resourceLeak
}
void resourceLeak_opendir(void)
{
// cppcheck-suppress unreadVariable
DIR* leak1 = opendir("abc");
// cppcheck-suppress resourceLeak
}
void resourceLeak_socket(void)
{
// cppcheck-suppress unreadVariable
int s = socket(AF_INET, SOCK_STREAM, 0);
// cppcheck-suppress resourceLeak
}
void noleak(int x, int y, int z)
{
DIR *p1 = fdopendir(x);
closedir(p1);
DIR *p2 = opendir("abc");
closedir(p2);
int s = socket(AF_INET,SOCK_STREAM,0);
close(s);
/* TODO: add configuration for open/fdopen
// #2830
int fd = open("path", O_RDONLY);
FILE *f = fdopen(fd, "rt");
fclose(f);
*/
}
// unused return value
void ignoredReturnValue(void *addr, int fd)
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
mmap(addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
// cppcheck-suppress ignoredReturnValue
setuid(42);
// cppcheck-suppress ignoredReturnValue
getuid();
}
// valid range
void invalidFunctionArg()
{
// cppcheck-suppress invalidFunctionArg
// cppcheck-suppress usleepCalled
usleep(-1);
// cppcheck-suppress usleepCalled
usleep(0);
// cppcheck-suppress usleepCalled
usleep(999999);
// cppcheck-suppress invalidFunctionArg
// cppcheck-suppress usleepCalled
usleep(1000000);
}
void uninitvar(int fd)
{
int x;
char buf[2];
int decimal, sign;
double d;
void *p;
// cppcheck-suppress uninitvar
write(x,"ab",2);
// cppcheck-suppress uninitvar
write(fd,buf,2); // #6325
// cppcheck-suppress uninitvar
write(fd,"ab",x);
// cppcheck-suppress uninitvar
write(fd,p,2);
/* int regcomp(regex_t *restrict preg, const char *restrict pattern, int cflags); */
regex_t reg;
const char * pattern;
int cflags;
// cppcheck-suppress uninitvar
regcomp(®, pattern, cflags);
pattern="";
// cppcheck-suppress uninitvar
regcomp(®, pattern, cflags);
regerror(0, ®, 0, 0);
// cppcheck-suppress uninitvar
// cppcheck-suppress unreadVariable
// cppcheck-suppress ecvtCalled
char *buffer = ecvt(d, 11, &decimal, &sign);
// cppcheck-suppress gcvtCalled
gcvt(3.141, 2, buf);
char *filename;
struct utimbuf *times;
// cppcheck-suppress uninitvar
// cppcheck-suppress utimeCalled
utime(filename, times);
struct timeval times1[2];
// cppcheck-suppress uninitvar
// cppcheck-suppress utimeCalled
utime(filename, times1);
}
void uninitvar_getcwd(void)
{
char *buf;
size_t size;
// cppcheck-suppress uninitvar
(void)getcwd(buf,size);
}
void uninitvar_types(void)
{
// cppcheck-suppress unassignedVariable
blkcnt_t b;
// cppcheck-suppress uninitvar
b + 1;
struct dirent d;
// cppcheck-suppress uninitvar
d.d_ino + 1;
}
void timet_h(struct timespec* ptp1)
{
clockid_t clk_id;
struct timespec* ptp;
// cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, ptp);
// cppcheck-suppress uninitvar
clock_settime(clk_id, ptp);
// cppcheck-suppress uninitvar
clock_settime(clk_id, ptp1);
struct timespec tp;
// cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, &tp); // #6577 - false negative
// cppcheck-suppress uninitvar
// cppcheck-suppress clock_settimeCalled
clock_settime(clk_id, &tp);
time_t clock = time(0);
char buf[26];
// cppcheck-suppress ctime_rCalled
ctime_r(&clock, buf);
}