aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/analyzer/fd-meaning.c
blob: 6a9ec921fd39c9005038af3f2408013aeb140f43 (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
  /* { dg-additional-options "-fanalyzer-verbose-state-changes" } */
int open(const char *, int mode);
void close(int fd);

#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2

void test_1 (const char* path)
{
    int fd = open (path, O_RDWR); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
    if (fd != -1)
    {
        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
    }
}

void test_2 (const char* path)
{
    int fd = open (path, O_RDONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
    if (fd != -1)
    {
        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
    }
}

void test_3 (const char* path)
{
    int fd = open (path, O_WRONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */
    if (fd != -1)
    {
        close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */
        close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */
    }
}