aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/fd-4.c
blob: 842a26b43647fc8b273a54b388359f68d203eb40 (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
98
99
100
101
102
103
104
#ifdef _AIX
#define _MODE_T
#endif
#include <stdio.h>

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);

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

typedef enum {
  S_IRWXU
  // etc
} mode_t;

int creat (const char *, mode_t mode);

void
test_1 (const char *path, void *buf)
{
    int fd = open (path, O_RDONLY); /* { dg-message "opened here as read-only" } */
    if (fd >= 0) /* { dg-message "assuming 'fd' is a valid file descriptor \\(>= 0\\)" "event1" } */
    /* { dg-message "following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */
    {
        write (fd, buf, 1); /* { dg-warning "'write' on read-only file descriptor 'fd'" "warning" } */
        /* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */
        /* { dg-message "\\(5\\) 'write' on read-only file descriptor 'fd'" "event2" { target *-*-* } .-2 } */
        close (fd);
    }
}

void
test_2 (const char *path, void *buf)
{
    int fd = open (path, O_WRONLY); /* { dg-message "opened here as write-only" } */
    if (fd >= 0) /* { dg-message "assuming 'fd' is a valid file descriptor \\(>= 0\\)" "event1" } */
    /* { dg-message "following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */
    {
        read (fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd'" "warning" } */
        /* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */
        /* { dg-message "\\(5\\) 'read' on write-only file descriptor 'fd'" "event2" { target *-*-* } .-2 } */
        close (fd);
    }
}


void 
test_3 (const char *path, void *buf)
{
    int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
    if (fd >= 0)
    {
        close(fd); /* {dg-message "\\(2\\) closed here"} */
        read(fd, buf, 1); /* { dg-warning "'read' on closed file descriptor 'fd'" }  */
        /* {dg-message "\\(3\\) 'read' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" {target *-*-*} .-1 } */
    }
}

void 
test_4 (const char *path, void *buf)
{
    int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
    if (fd >= 0)
    {
        close(fd); /* {dg-message "\\(2\\) closed here"} */
        write(fd, buf, 1); /* { dg-warning "'write' on closed file descriptor 'fd'" }  */
        /* {dg-message "\\(3\\) 'write' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" {target *-*-*} .-1 } */
    }
}

void
test_5 (const char *path)
{
    int fd = open (path, O_RDWR);
    close(fd);
    printf("%d", fd); /* { dg-bogus "'printf' on a closed file descriptor 'fd'" } */
}


void
test_6 (const char *path, mode_t mode, void *buf)
{
  int fd = creat (path, mode);
  if (fd != -1)
  {
    read (fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd'" } */
    close(fd);
  }
}

void
test_7 (const char *path, mode_t mode, void *buf)
{
  int fd = creat (path, mode);
  if (fd != -1)
  {
    write (fd, buf, 1);
    close(fd);
  }
}