Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/fd-accept.c

raw
1 /* { dg-require-effective-target sockets } */
2 /* { dg-skip-if "" { powerpc*-*-aix* } } */
3 /* { dg-skip-if "PR analyzer/107750" { *-*-solaris2* } } */
4
5 #include <sys/socket.h>
6 #include <sys/un.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include "analyzer-decls.h"
10
11 int test_accept (int fd, struct sockaddr *addr, socklen_t *addrlen)
12 {
13 return accept (fd, addr, addrlen);
14 }
15
16 void test_accept_leak_no_lhs (int fd, struct sockaddr *addr, socklen_t *addrlen)
17 {
18 accept (fd, addr, addrlen); /* { dg-warning "leak of file descriptor" } */
19 }
20
21 void test_accept_leak_with_lhs (int fd, struct sockaddr *addr, socklen_t *addrlen)
22 {
23 int newfd = accept (fd, addr, addrlen); /* { dg-message "socket created here" } */
24 } /* { dg-warning "leak of file descriptor 'newfd'" } */
25
26 int test_accept_null_addr (int fd)
27 {
28 return accept (fd, NULL, 0);
29 }
30
31 int test_accept_uninit_addrlen (int fd)
32 {
33 struct sockaddr_storage addr;
34 socklen_t addr_len;
35 return accept (fd, (struct sockaddr *)&addr, &addr_len); /* { dg-warning "use of uninitialized value 'addr_len'" } */
36 }
37
38 int test_accept_writes_to_addr_and_len (int fd)
39 {
40 struct sockaddr_storage addr;
41 socklen_t addr_len = sizeof (addr);
42 __analyzer_eval (addr_len == sizeof (addr)); /* { dg-warning "TRUE" } */
43 int newfd = accept (fd, (struct sockaddr *)&addr, &addr_len);
44 if (newfd == -1)
45 return newfd;
46 /* Check that the analyzer considers addr and addr_len to
47 have been written to. */
48 __analyzer_eval (((char *)&addr)[0]); /* { dg-warning "UNKNOWN" } */
49 __analyzer_eval (addr_len == sizeof (addr)); /* { dg-warning "UNKNOWN" } */
50 return newfd;
51 }
52
53 void test_accept_on_new_datagram_socket (void)
54 {
55 int fd = socket (AF_UNIX, SOCK_DGRAM, 0);
56 if (fd == -1)
57 return;
58 accept (fd, NULL, NULL); /* { dg-message "'accept' on datagram socket file descriptor 'fd' \\\[-Wanalyzer-fd-type-mismatch\\\]" "warning" } */
59 /* { dg-message "'accept' expects a stream socket file descriptor but 'fd' is a datagram socket" "final event" { target *-*-* } .-1 } */
60 close (fd);
61 }
62
63 int test_accept_on_accept (int fd_a)
64 {
65 int fd_b = accept (fd_a, NULL, 0);
66 if (fd_b == -1)
67 return -1;
68
69 int fd_c = accept (fd_b, NULL, 0); /* { dg-warning "'accept' on file descriptor 'fd_b' in wrong phase \\\[CWE-666\\\] \\\[-Wanalyzer-fd-phase-mismatch\\\]" "warning" } */
70 /* { dg-message "'accept' expects a listening stream socket file descriptor but 'fd_b' is connected" "final event" { target *-*-* } .-1 } */
71
72 return fd_b;
73 }
74
75 int test_accept_on_constant ()
76 {
77 return accept (0, NULL, 0);
78 }