| 1 |
int open(const char *, int mode); |
| 2 |
void close(int fd); |
| 3 |
int dup (int old_fd); |
| 4 |
int dup2 (int old_fd, int new_fd); |
| 5 |
int dup3 (int old_fd, int new_fd, int flags); |
| 6 |
int write (int fd, void *buf, int nbytes); |
| 7 |
int read (int fd, void *buf, int nbytes); |
| 8 |
#define O_RDONLY 0 |
| 9 |
#define O_WRONLY 1 |
| 10 |
#define O_RDWR 2 |
| 11 |
#define O_ACCMODE 3 |
| 12 |
|
| 13 |
void test_1 (const char *path) |
| 14 |
{ |
| 15 |
int old_fd = open (path, O_RDWR); |
| 16 |
int new_fd = dup (old_fd); /* { dg-warning "'dup' on possibly invalid file descriptor 'old_fd'" } */ |
| 17 |
close(old_fd); |
| 18 |
close(new_fd); |
| 19 |
} |
| 20 |
|
| 21 |
void test_2 (const char *path) |
| 22 |
{ |
| 23 |
int old_fd = open (path, O_RDWR); |
| 24 |
if (old_fd != -1) |
| 25 |
{ |
| 26 |
int new_fd = dup (old_fd); |
| 27 |
close(old_fd); |
| 28 |
return; /* { dg-warning "leak of file descriptor 'new_fd' \\\[CWE-775\\\]" } */ |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
void test_3 (const char *path, void *buf) |
| 33 |
{ |
| 34 |
int old_fd = open (path, O_RDWR); |
| 35 |
if (old_fd != -1) |
| 36 |
{ |
| 37 |
int new_fd = dup (old_fd); |
| 38 |
write (new_fd, buf, 1); /* { dg-warning "'write' on possibly invalid file descriptor 'new_fd'" } */ |
| 39 |
close (new_fd); |
| 40 |
close(old_fd); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
void test_5 (const char *path, void *buf) |
| 46 |
{ |
| 47 |
int old_fd = open (path, O_RDWR); |
| 48 |
if (old_fd != -1) |
| 49 |
{ |
| 50 |
int new_fd = dup (old_fd); |
| 51 |
if (new_fd != -1) |
| 52 |
{ |
| 53 |
write (new_fd, buf, 1); |
| 54 |
close (new_fd); |
| 55 |
|
| 56 |
} |
| 57 |
close(old_fd); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
void test_7 (const char *path) |
| 63 |
{ |
| 64 |
int old_fd = open (path, O_RDWR); |
| 65 |
dup2 (old_fd, 4); /* { dg-warning "'dup2' on possibly invalid file descriptor 'old_fd'" } */ |
| 66 |
close(old_fd); |
| 67 |
} |
| 68 |
|
| 69 |
void test_8 (const char *path) |
| 70 |
{ |
| 71 |
int old_fd = open (path, O_RDWR); |
| 72 |
int new_fd = open (path, O_RDWR); |
| 73 |
if (old_fd != -1) |
| 74 |
{ |
| 75 |
dup2 (old_fd, new_fd); /* { dg-warning "'dup2' on possibly invalid file descriptor 'new_fd'" } */ |
| 76 |
close (old_fd); |
| 77 |
} |
| 78 |
close (new_fd); |
| 79 |
} |
| 80 |
|
| 81 |
void test_9 (const char *path, void *buf) |
| 82 |
{ |
| 83 |
int old_fd = open (path, O_RDWR); |
| 84 |
|
| 85 |
if (old_fd != -1) |
| 86 |
{ |
| 87 |
int new_fd = open (path, O_RDWR); |
| 88 |
if (new_fd != -1) |
| 89 |
{ |
| 90 |
int lhs = dup2 (old_fd, new_fd); |
| 91 |
write (lhs, buf, 1); /* { dg-warning "'write' on possibly invalid file descriptor 'lhs'" } */ |
| 92 |
close(new_fd); |
| 93 |
close(lhs); |
| 94 |
} |
| 95 |
close(old_fd); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void test_10 (const char *path, int flags) |
| 100 |
{ |
| 101 |
int old_fd = open (path, O_RDWR); |
| 102 |
int new_fd = open (path, O_RDWR); |
| 103 |
if (old_fd != -1) |
| 104 |
{ |
| 105 |
dup3 (old_fd, new_fd, flags); /* { dg-warning "'dup3' on possibly invalid file descriptor 'new_fd'" } */ |
| 106 |
close(old_fd); |
| 107 |
|
| 108 |
} |
| 109 |
close(new_fd); |
| 110 |
} |
| 111 |
|
| 112 |
void test_11 (const char *path, int flags) |
| 113 |
{ |
| 114 |
int old_fd = open (path, O_RDWR); |
| 115 |
int new_fd = open (path, O_RDWR); |
| 116 |
if (new_fd != -1) |
| 117 |
{ |
| 118 |
dup3 (old_fd, new_fd, flags); /* { dg-warning "'dup3' on possibly invalid file descriptor 'old_fd'" } */ |
| 119 |
close(new_fd); |
| 120 |
|
| 121 |
} |
| 122 |
close(old_fd); |
| 123 |
} |
| 124 |
|
| 125 |
void test_12 (const char *path, void *buf) |
| 126 |
{ |
| 127 |
int old_fd = open (path, O_RDONLY); |
| 128 |
if (old_fd != -1) |
| 129 |
{ |
| 130 |
int new_fd = dup (old_fd); |
| 131 |
if (new_fd != -1) |
| 132 |
{ |
| 133 |
write (new_fd, buf, 1); /* { dg-warning "'write' on read-only file descriptor 'new_fd'" } */ |
| 134 |
close(new_fd); |
| 135 |
} |
| 136 |
close(old_fd); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void test_13 (const char *path, void *buf) |
| 141 |
{ |
| 142 |
int old_fd = open (path, O_WRONLY); |
| 143 |
if (old_fd != -1) |
| 144 |
{ |
| 145 |
int new_fd = dup (old_fd); |
| 146 |
if (new_fd != -1) |
| 147 |
{ |
| 148 |
read (new_fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'new_fd'" } */ |
| 149 |
close(new_fd); |
| 150 |
} |
| 151 |
close(old_fd); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void test_14 (const char *path, void *buf) |
| 156 |
{ |
| 157 |
int old_fd = open (path, O_RDWR); |
| 158 |
if (old_fd != -1) |
| 159 |
{ |
| 160 |
int new_fd = dup (old_fd); |
| 161 |
if (new_fd != -1) |
| 162 |
{ |
| 163 |
write (new_fd, buf, 1); |
| 164 |
read (new_fd, buf, 1); |
| 165 |
close(new_fd); |
| 166 |
} |
| 167 |
close(old_fd); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
void test_15 (void *buf) |
| 172 |
{ |
| 173 |
int fd = dup(0); |
| 174 |
read (fd, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" } */ |
| 175 |
close(fd); |
| 176 |
} |
| 177 |
|
| 178 |
void test_16 (void *buf) |
| 179 |
{ |
| 180 |
int fd = dup(1); |
| 181 |
if (fd != -1) |
| 182 |
{ |
| 183 |
write (fd, buf, 1); |
| 184 |
close (fd); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void test_17 (const char *path) |
| 189 |
{ |
| 190 |
int fd = open (path, O_RDWR); |
| 191 |
close(fd); |
| 192 |
dup (fd); /* { dg-warning "'dup' on closed file descriptor 'fd'" } */ |
| 193 |
dup2 (fd, 4); /* { dg-warning "'dup2' on closed file descriptor 'fd'" } */ |
| 194 |
} |
| 195 |
|
| 196 |
void |
| 197 |
test_18 (const char *path, void *buf) |
| 198 |
{ |
| 199 |
int fd = open (path, O_RDWR); |
| 200 |
if (fd != -1) |
| 201 |
{ |
| 202 |
int fd2 = dup2 (fd, 3); |
| 203 |
read (fd2, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd2'" } */ |
| 204 |
close(fd); |
| 205 |
close(fd2); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void |
| 210 |
test_19 (const char *path, void *buf) |
| 211 |
{ |
| 212 |
int fd = open (path, O_WRONLY); |
| 213 |
if (fd != -1) |
| 214 |
{ |
| 215 |
int fd2 = dup2 (fd, 4); |
| 216 |
if (fd2 != -1) |
| 217 |
{ |
| 218 |
read (fd2, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd2'" } */ |
| 219 |
close(fd2); |
| 220 |
} |
| 221 |
close (fd); |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
extern int m; |
| 227 |
|
| 228 |
void |
| 229 |
test_20 () |
| 230 |
{ |
| 231 |
int fd = dup (m); |
| 232 |
close (fd); |
| 233 |
} |
| 234 |
|
| 235 |
void |
| 236 |
test_21 () |
| 237 |
{ |
| 238 |
int fd = dup2 (m, 1); |
| 239 |
close (fd); |
| 240 |
} |
| 241 |
|
| 242 |
void |
| 243 |
test_22 (int flags) |
| 244 |
{ |
| 245 |
int fd = dup3 (m, 1, flags); |
| 246 |
close (fd); |
| 247 |
} |
| 248 |
|
| 249 |
void do_something(); |
| 250 |
void |
| 251 |
test_23 () |
| 252 |
{ |
| 253 |
int nullfd = -1; |
| 254 |
int fd = 1; |
| 255 |
if (dup2 (nullfd, fd) < 0) /* { dg-warning "'dup2' on possibly invalid file descriptor 'nullfd'" } */ |
| 256 |
{ |
| 257 |
do_something(); |
| 258 |
} |
| 259 |
} |
| 260 |
|