Testsuite: fix analyzer tests on Darwin
[official-gcc.git] / gcc / testsuite / gcc.dg / analyzer / fd-listen.c
blob3ac7a990042c7758638b6769b7161c80fc6768d2
1 /* { dg-require-effective-target sockets } */
2 /* { dg-skip-if "" { powerpc*-*-aix* } } */
4 #include <string.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include "analyzer-decls.h"
11 int test_listen (int fd, int backlog)
13 return listen (fd, backlog);
16 /* Some systems seem to allow repeated calls to listen. */
18 void test_double_listen (int fd, int backlog)
20 listen (fd, backlog);
21 listen (fd, backlog);
24 void test_listen_before_bind (int fd, const char *sockname)
26 if (listen (fd, 5) == -1) /* { dg-message "stream socket marked as passive here via 'listen'" } */
27 return;
29 struct sockaddr_un addr;
30 memset (&addr, 0, sizeof (addr));
31 addr.sun_family = AF_UNIX;
32 strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
33 bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-warning "'bind' on file descriptor 'fd' in wrong phase" "warning" } */
34 /* { dg-message "'bind' expects a new socket file descriptor but 'fd' is already listening" "final event" { target *-*-* } .-1 } */
37 void test_listen_on_unchecked_bind (int fd, const char *sockname)
39 struct sockaddr_un addr;
40 memset (&addr, 0, sizeof (addr));
41 addr.sun_family = AF_UNIX;
42 strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
43 bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-message "when 'bind' fails" } */
44 listen (fd, 5); /* { dg-warning "'listen' on file descriptor 'fd' in wrong phase" "warning" } */
45 /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "final event" { target *-*-* } .-1 } */
46 close (fd);
49 void test_listen_on_new_datagram_socket (void)
51 int fd = socket (AF_UNIX, SOCK_DGRAM, 0);
52 if (fd == -1)
53 return;
54 listen (fd, 5); /* { dg-message "'listen' on datagram socket file descriptor 'fd' \\\[-Wanalyzer-fd-type-mismatch\\\]" "warning" } */
55 /* { dg-message "'listen' expects a stream socket file descriptor but 'fd' is a datagram socket" "final event" { target *-*-* } .-1 } */
56 close (fd);
59 void test_listen_on_connected_socket (int fd)
61 int afd = accept (fd, NULL, 0);
62 if (afd == -1)
63 return;
64 listen (afd, 5); /* { dg-warning "'listen' on file descriptor 'afd' in wrong phase" "warning" } */
65 /* { dg-message "'listen' expects a bound stream socket file descriptor but 'afd' is connected" "final event" { target *-*-* } .-1 } */
66 close (afd);
69 int test_listen_on_constant ()
71 return listen (0, 10);