aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/analyzer/fd-3.c
blob: 8e71b14f71b69502390fd100d13b166a034c3dfa (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
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
int open(const char *, int mode);
void close(int fd);
int write (int fd, void *buf, int nbytes);
int read (int fd, void *buf, int nbytes);
int some_condition();

#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define STDIN 0
#define O_NOATIME 262144

void
test_1 (const char *path, void *buf)
{
    int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
    write (fd, buf, 1); /* { dg-message "\\(2\\) 'fd' could be invalid: unchecked value from \\(1\\)" } */
    /* { dg-warning "'write' on possibly invalid file descriptor 'fd'" "warning" { target *-*-* } .-1 } */
    close(fd);
}

void
test_2 (const char *path, void *buf)
{
    int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
    read (fd, buf, 1); /* { dg-message "\\(2\\) 'fd' could be invalid: unchecked value from \\(1\\)" } */
    /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" "warning" { target *-*-* } .-1 } */
    close (fd);
}

void 
test_3 (void *buf)
{
    int fd = -1;
    read (fd, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" } */
    /* { dg-message "\\(1\\) 'fd' could be invalid" "" { target *-*-* } .-1 } */
}

void
test_4 (void *buf)
{
    int fd = STDIN;
    read (fd, buf, 1);
    close(fd);
}

void
test_5 (char *path, void *buf)
{
    int flags = O_RDONLY;
    if (some_condition())
        flags |= O_NOATIME;
    int fd = open (path, flags); /* { dg-message "\\(1\\) opened here" } */
    read (fd, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" } */
    /* { dg-message "\\(2\\) 'fd' could be invalid" "" { target *-*-* } .-1 } */
    close (fd);   
}


void
test_6 (char *path, void *buf)
{
    int fd = open (path, O_RDONLY);
    if (fd != -1)
    {
        read (fd, buf, 1);
    }
    close (fd);
}


void
test_7 (char *path, void *buf)
{
    int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
    if (fd != -1) /* { dg-message "\\(2\\) assuming 'fd' is an invalid file descriptor \\(< 0\\)" } */
    {
        read (fd, buf, 1);
    } else
    {
        write (fd, buf, 1); /* { dg-warning "'write' on possibly invalid file descriptor 'fd'" } */
        
    }
    close(fd);
}

void
test_read_from_symbolic_fd (int fd, void *buf)
{
  read (fd, buf, 1);
}

void
test_write_to_symbolic_fd (int fd, void *buf)
{
  write (fd, buf, 1);
}