doc: Use https for our own site (and GCC for the project)
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / fd-symbolic-socket.c
blobc6af0a6487f462452ab895487954000d77dd6ec7
1 /* { dg-require-effective-target sockets } */
3 /* Needed on some targets until we have exception-handling working (PR 111475). */
4 /* { dg-additional-options "-fno-exceptions" } */
6 /* { dg-skip-if "" { hppa*-*-hpux* powerpc*-*-aix* } } */
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include "analyzer-decls.h"
15 void test_leak_socket (int type)
17 int fd = socket (AF_UNIX, type, 0); /* { dg-message "socket created here" } */
18 } /* { dg-warning "leak of file descriptor 'fd'" } */
20 void test_leak_socket_no_lhs (int type)
22 socket (AF_UNIX, type, 0); /* { dg-warning "leak of file descriptor" } */
25 void test_close_unchecked_socket (int type)
27 int fd = socket (AF_UNIX, type, 0);
28 close (fd);
31 void test_close_checked_socket (int type)
33 int fd = socket (AF_UNIX, type, 0);
34 if (fd == -1)
35 return;
36 close (fd);
39 void test_leak_checked_socket (int type)
41 int fd = socket (AF_UNIX, type, 0); /* { dg-message "socket created here" } */
42 if (fd == -1) /* { dg-warning "leak of file descriptor 'fd'" } */
43 return;
44 // TODO: strange location for leak message
47 void test_bind_on_checked_socket (int type, const char *sockname)
49 struct sockaddr_un addr;
50 int fd = socket (AF_UNIX, type, 0);
51 if (fd == -1)
52 return;
53 memset (&addr, 0, sizeof (addr));
54 addr.sun_family = AF_UNIX;
55 strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
56 bind (fd, (struct sockaddr *)&addr, sizeof (addr));
57 close (fd);
60 void test_bind_on_unchecked_socket (int type, const char *sockname)
62 struct sockaddr_un addr;
63 int fd = socket (AF_UNIX, type, 0); /* { dg-message "when 'socket' fails" } */
64 memset (&addr, 0, sizeof (addr));
65 addr.sun_family = AF_UNIX;
66 strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
67 bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-warning "'bind' on possibly invalid file descriptor 'fd'" } */
68 close (fd);
71 void test_leak_of_bound_socket (int type, const char *sockname)
73 struct sockaddr_un addr;
74 int fd = socket (AF_UNIX, type, 0); /* { dg-message "socket created here" } */
75 if (fd == -1)
76 return;
77 memset (&addr, 0, sizeof (addr));
78 addr.sun_family = AF_UNIX;
79 strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
80 bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-warning "leak of file descriptor 'fd'" } */
83 void test_listen_without_bind (int type)
85 int fd = socket (AF_UNIX, type, 0);
86 if (fd == -1)
87 return;
88 listen (fd, 5); /* { dg-warning "'listen' on file descriptor 'fd' in wrong phase" } */
89 /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "msg" { target *-*-* } .-1 } */
90 close (fd);
93 void test_listen_on_unchecked_bind (int type, const char *sockname)
95 struct sockaddr_un addr;
96 int fd = socket (AF_UNIX, type, 0);
97 if (fd == -1)
98 return;
99 memset (&addr, 0, sizeof (addr));
100 addr.sun_family = AF_UNIX;
101 strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
102 bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-message "when 'bind' fails" } */
103 listen (fd, 5); /* { dg-warning "'listen' on file descriptor 'fd' in wrong phase" "warning" } */
104 /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "msg" { target *-*-* } .-1 } */
105 close (fd);