Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/fd-access-mode-enum.c

raw
1 int open(const char *, int mode);
2 void close(int fd);
3 int write (int fd, void *buf, int nbytes);
4 int read (int fd, void *buf, int nbytes);
5
6 /* Example of these flags as an enum, and with
7 non-standard values for them. */
8
9 enum {
10 O_RDONLY = 0x10,
11 O_WRONLY = 0x20,
12 O_RDWR = 0x40,
13
14 O_ACCMODE = 0xf0
15 };
16
17 void f (int fd) __attribute__((fd_arg(1))); /* { dg-message "argument 1 of 'f' must be an open file descriptor, due to '__attribute__\\(\\(fd_arg\\(1\\)\\)\\)'" } */
18
19 void
20 test_1 (const char *path)
21 {
22 int fd = open (path, O_RDWR);
23 close(fd);
24 f(fd); /* { dg-warning "'f' on closed file descriptor 'fd'" } */
25 /* { dg-message "\\(3\\) 'f' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" { target *-*-* } .-1 } */
26 }
27
28 void g (int fd) __attribute__((fd_arg_read(1))); /* { dg-message "argument 1 of 'g' must be a readable file descriptor, due to '__attribute__\\(\\(fd_arg_read\\(1\\)\\)\\)'" } */
29
30 void
31 test_2 (const char *path)
32 {
33 int fd = open (path, O_WRONLY);
34 if (fd != -1)
35 {
36 g (fd); /* { dg-warning "'g' on write-only file descriptor 'fd'" } */
37 }
38 close (fd);
39 }
40
41 void h (int fd) __attribute__((fd_arg_write(1))); /* { dg-message "argument 1 of 'h' must be a writable file descriptor, due to '__attribute__\\(\\(fd_arg_write\\(1\\)\\)\\)'" } */
42 void
43 test_3 (const char *path)
44 {
45 int fd = open (path, O_RDONLY);
46 if (fd != -1)
47 {
48 h (fd); /* { dg-warning "'h' on read-only file descriptor 'fd'" } */
49 }
50 close(fd);
51 }
52
53 void ff (int fd) __attribute__((fd_arg(1))); /* { dg-message "argument 1 of 'ff' must be an open file descriptor, due to '__attribute__\\(\\(fd_arg\\(1\\)\\)\\)'" } */
54
55 void test_4 (const char *path)
56 {
57 int fd = open (path, O_RDWR);
58 ff (fd); /* { dg-warning "'ff' on possibly invalid file descriptor 'fd'" } */
59 close(fd);
60 }