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* } } */
9 #include <sys/socket.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);
31 void test_close_checked_socket (int type
)
33 int fd
= socket (AF_UNIX
, type
, 0);
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'" } */
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);
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
));
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'" } */
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" } */
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);
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 } */
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);
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 } */