| 1 |
int open(const char *, int mode); |
| 2 |
#define O_RDONLY 0 |
| 3 |
#define O_WRONLY 1 |
| 4 |
#define O_RDWR 2 |
| 5 |
|
| 6 |
typedef enum { |
| 7 |
S_IRWXU |
| 8 |
// etc |
| 9 |
} mode_t; |
| 10 |
|
| 11 |
int creat (const char *, mode_t mode); |
| 12 |
|
| 13 |
void |
| 14 |
test_1 (const char *path) |
| 15 |
{ |
| 16 |
int fd = open (path, O_RDONLY); /* { dg-message "\\(1\\) opened here" } */ |
| 17 |
return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" "warning" } */ |
| 18 |
/* { dg-message "\\(2\\) 'fd' leaks here; was opened at \\(1\\)" "event" { target *-*-* } .-1 } */ |
| 19 |
} |
| 20 |
|
| 21 |
void |
| 22 |
test_2 (const char *path) |
| 23 |
{ |
| 24 |
int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */ |
| 25 |
if (fd >= 0) /* { dg-message "\\(2\\) assuming 'fd' is a valid file descriptor" "event1" } */ |
| 26 |
/* { dg-message "\\(3\\) following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */ |
| 27 |
{ |
| 28 |
return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" "warning" } */ |
| 29 |
/* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */ |
| 30 |
/* { dg-message "\\(5\\) 'fd' leaks here; was opened at \\(1\\)" "event2" { target *-*-* } .-2 } */ |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
void |
| 35 |
test_3 (const char *path) |
| 36 |
{ |
| 37 |
int fd = open (path, O_WRONLY); /* { dg-message "\\(1\\) opened here" } */ |
| 38 |
return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" "warning" } */ |
| 39 |
} |
| 40 |
|
| 41 |
void test_4 (const char *path) |
| 42 |
{ |
| 43 |
open(path, O_RDONLY); /* { dg-warning "leak of file descriptor \\\[CWE-775\\\]" } */ |
| 44 |
/* { dg-message "\\(1\\) leaks here" "" { target *-*-* } .-1 } */ |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
test_5 (const char *path, mode_t mode) |
| 49 |
{ |
| 50 |
creat (path, mode); /* { dg-warning "leak of file descriptor \\\[CWE-775\\\]" } */ |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
test_6 (const char *path, mode_t mode) |
| 55 |
{ |
| 56 |
int fd = creat (path, mode); |
| 57 |
return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" } */ |
| 58 |
} |
| 59 |
|
| 60 |
|