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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
int open(const char *, int mode);
void close(int fd);
int dup (int old_fd);
int dup2 (int old_fd, int new_fd);
int dup3 (int old_fd, int new_fd, int flags);
int write (int fd, void *buf, int nbytes);
int read (int fd, void *buf, int nbytes);
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_ACCMODE 3
void test_1 (const char *path)
{
int old_fd = open (path, O_RDWR);
int new_fd = dup (old_fd); /* { dg-warning "'dup' on possibly invalid file descriptor 'old_fd'" } */
close(old_fd);
close(new_fd);
}
void test_2 (const char *path)
{
int old_fd = open (path, O_RDWR);
if (old_fd != -1)
{
int new_fd = dup (old_fd);
close(old_fd);
return; /* { dg-warning "leak of file descriptor 'new_fd' \\\[CWE-775\\\]" } */
}
}
void test_3 (const char *path, void *buf)
{
int old_fd = open (path, O_RDWR);
if (old_fd != -1)
{
int new_fd = dup (old_fd);
write (new_fd, buf, 1); /* { dg-warning "'write' on possibly invalid file descriptor 'new_fd'" } */
close (new_fd);
close(old_fd);
}
}
void test_5 (const char *path, void *buf)
{
int old_fd = open (path, O_RDWR);
if (old_fd != -1)
{
int new_fd = dup (old_fd);
if (new_fd != -1)
{
write (new_fd, buf, 1);
close (new_fd);
}
close(old_fd);
}
}
void test_7 (const char *path)
{
int old_fd = open (path, O_RDWR);
dup2 (old_fd, 4); /* { dg-warning "'dup2' on possibly invalid file descriptor 'old_fd'" } */
close(old_fd);
}
void test_8 (const char *path)
{
int old_fd = open (path, O_RDWR);
int new_fd = open (path, O_RDWR);
if (old_fd != -1)
{
dup2 (old_fd, new_fd); /* { dg-warning "'dup2' on possibly invalid file descriptor 'new_fd'" } */
close (old_fd);
}
close (new_fd);
}
void test_9 (const char *path, void *buf)
{
int old_fd = open (path, O_RDWR);
if (old_fd != -1)
{
int new_fd = open (path, O_RDWR);
if (new_fd != -1)
{
int lhs = dup2 (old_fd, new_fd);
write (lhs, buf, 1); /* { dg-warning "'write' on possibly invalid file descriptor 'lhs'" } */
close(new_fd);
close(lhs);
}
close(old_fd);
}
}
void test_10 (const char *path, int flags)
{
int old_fd = open (path, O_RDWR);
int new_fd = open (path, O_RDWR);
if (old_fd != -1)
{
dup3 (old_fd, new_fd, flags); /* { dg-warning "'dup3' on possibly invalid file descriptor 'new_fd'" } */
close(old_fd);
}
close(new_fd);
}
void test_11 (const char *path, int flags)
{
int old_fd = open (path, O_RDWR);
int new_fd = open (path, O_RDWR);
if (new_fd != -1)
{
dup3 (old_fd, new_fd, flags); /* { dg-warning "'dup3' on possibly invalid file descriptor 'old_fd'" } */
close(new_fd);
}
close(old_fd);
}
void test_12 (const char *path, void *buf)
{
int old_fd = open (path, O_RDONLY);
if (old_fd != -1)
{
int new_fd = dup (old_fd);
if (new_fd != -1)
{
write (new_fd, buf, 1); /* { dg-warning "'write' on read-only file descriptor 'new_fd'" } */
close(new_fd);
}
close(old_fd);
}
}
void test_13 (const char *path, void *buf)
{
int old_fd = open (path, O_WRONLY);
if (old_fd != -1)
{
int new_fd = dup (old_fd);
if (new_fd != -1)
{
read (new_fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'new_fd'" } */
close(new_fd);
}
close(old_fd);
}
}
void test_14 (const char *path, void *buf)
{
int old_fd = open (path, O_RDWR);
if (old_fd != -1)
{
int new_fd = dup (old_fd);
if (new_fd != -1)
{
write (new_fd, buf, 1);
read (new_fd, buf, 1);
close(new_fd);
}
close(old_fd);
}
}
void test_15 (void *buf)
{
int fd = dup(0);
read (fd, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" } */
close(fd);
}
void test_16 (void *buf)
{
int fd = dup(1);
if (fd != -1)
{
write (fd, buf, 1);
close (fd);
}
}
void test_17 (const char *path)
{
int fd = open (path, O_RDWR);
close(fd);
dup (fd); /* { dg-warning "'dup' on closed file descriptor 'fd'" } */
dup2 (fd, 4); /* { dg-warning "'dup2' on closed file descriptor 'fd'" } */
}
void
test_18 (const char *path, void *buf)
{
int fd = open (path, O_RDWR);
if (fd != -1)
{
int fd2 = dup2 (fd, 3);
read (fd2, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd2'" } */
close(fd);
close(fd2);
}
}
void
test_19 (const char *path, void *buf)
{
int fd = open (path, O_WRONLY);
if (fd != -1)
{
int fd2 = dup2 (fd, 4);
if (fd2 != -1)
{
read (fd2, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd2'" } */
close(fd2);
}
close (fd);
}
}
extern int m;
void
test_20 ()
{
int fd = dup (m);
close (fd);
}
void
test_21 ()
{
int fd = dup2 (m, 1);
close (fd);
}
void
test_22 (int flags)
{
int fd = dup3 (m, 1, flags);
close (fd);
}
void do_something();
void
test_23 ()
{
int nullfd = -1;
int fd = 1;
if (dup2 (nullfd, fd) < 0) /* { dg-warning "'dup2' on possibly invalid file descriptor 'nullfd'" } */
{
do_something();
}
}
|