aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/fd-2.c
blob: 10c9ecdb09d97873f12ecca6dabefc0b8169ec85 (plain)
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
int open(const char *, int mode);
void close(int fd);
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define STDIN 0

typedef enum {
  S_IRWXU
  // etc
} mode_t;

int creat (const char *, mode_t mode);

void 
test_1 (const char *path)
{
    int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
    close (fd); /* { dg-message "\\(2\\) first 'close' here" "event1" } */
    close (fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" "warning" } */
    /* { dg-message "\\(3\\) second 'close' here; first 'close' was at \\(2\\)" "event2" { target *-*-* } .-1 } */
}

void 
test_2 (const char *path)
{
    int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
    if (fd < 0) /* { dg-message "\\(2\\) assuming 'fd' is a valid file descriptor \\(>= 0\\)" "event1" } */
    /* { dg-message "\\(3\\) following 'false' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */
        return;
    close (fd); /* { dg-message "\\(4\\) ...to here" "event1" } */
    /* { dg-message "\\(5\\) first 'close' here" "event2" { target *-*-* } .-1 } */
    close (fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" "warning" } */
    /* {dg-message "\\(6\\) second 'close' here; first was at \\(5\\)" "" { target *-*-* } .-1 } */
}

void
test_3 ()
{
    /* FD 0 is stdin at the entry to "main" and thus read-only, but we have no
    guarantees here that it hasn't been closed and then reopened for
    writing, so we can't issue a warning */
    
    int fd = STDIN;
    close(fd); /* { dg-message "\\(1\\) first 'close' here" } */
    close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" "warning" } */
     /* { dg-message "\\(2\\) second 'close' here; first 'close' was at \\(1\\)" "event2" { target *-*-* } .-1 } */
}

void
test_4 ()
{
    int fd = -1;
    close(fd);
    close(fd);
}

void
test_5 (const char *path, mode_t mode)
{
    int fd = creat (path, mode);
    close(fd);
    close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" "warning" } */
}