Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/fd-access-mode-macros.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 #define O_RDONLY 0
7 #define O_WRONLY 1
8 #define O_RDWR 2
9 #define O_ACCMODE 0x3
10
11 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\\)\\)\\)'" } */
12
13 void
14 test_1 (const char *path)
15 {
16 int fd = open (path, O_RDWR);
17 close(fd);
18 f(fd); /* { dg-warning "'f' on closed file descriptor 'fd'" } */
19 /* { dg-message "\\(3\\) 'f' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" { target *-*-* } .-1 } */
20 }
21
22 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\\)\\)\\)'" } */
23
24 void
25 test_2 (const char *path)
26 {
27 int fd = open (path, O_WRONLY);
28 if (fd != -1)
29 {
30 g (fd); /* { dg-warning "'g' on write-only file descriptor 'fd'" } */
31 }
32 close (fd);
33 }
34
35 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\\)\\)\\)'" } */
36 void
37 test_3 (const char *path)
38 {
39 int fd = open (path, O_RDONLY);
40 if (fd != -1)
41 {
42 h (fd); /* { dg-warning "'h' on read-only file descriptor 'fd'" } */
43 }
44 close(fd);
45 }
46
47 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\\)\\)\\)'" } */
48
49 void test_4 (const char *path)
50 {
51 int fd = open (path, O_RDWR);
52 ff (fd); /* { dg-warning "'ff' on possibly invalid file descriptor 'fd'" } */
53 close(fd);
54 }