1 /* { dg-require-effective-target sockets } */
2 /* { dg-skip-if "" { powerpc*-*-aix* } } */
5 #include <sys/socket.h>
9 #include "analyzer-decls.h"
11 void test_leak_socket (int type
)
13 int fd
= socket (AF_UNIX
, type
, 0); /* { dg-message "socket created here" } */
14 } /* { dg-warning "leak of file descriptor 'fd'" } */
16 void test_leak_socket_no_lhs (int type
)
18 socket (AF_UNIX
, type
, 0); /* { dg-warning "leak of file descriptor" } */
21 void test_close_unchecked_socket (int type
)
23 int fd
= socket (AF_UNIX
, type
, 0);
27 void test_close_checked_socket (int type
)
29 int fd
= socket (AF_UNIX
, type
, 0);
35 void test_leak_checked_socket (int type
)
37 int fd
= socket (AF_UNIX
, type
, 0); /* { dg-message "socket created here" } */
38 if (fd
== -1) /* { dg-warning "leak of file descriptor 'fd'" } */
40 // TODO: strange location for leak message
43 void test_bind_on_checked_socket (int type
, const char *sockname
)
45 struct sockaddr_un addr
;
46 int fd
= socket (AF_UNIX
, type
, 0);
49 memset (&addr
, 0, sizeof (addr
));
50 addr
.sun_family
= AF_UNIX
;
51 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
52 bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
));
56 void test_bind_on_unchecked_socket (int type
, const char *sockname
)
58 struct sockaddr_un addr
;
59 int fd
= socket (AF_UNIX
, type
, 0); /* { dg-message "when 'socket' fails" } */
60 memset (&addr
, 0, sizeof (addr
));
61 addr
.sun_family
= AF_UNIX
;
62 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
63 bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
)); /* { dg-warning "'bind' on possibly invalid file descriptor 'fd'" } */
67 void test_leak_of_bound_socket (int type
, const char *sockname
)
69 struct sockaddr_un addr
;
70 int fd
= socket (AF_UNIX
, type
, 0); /* { dg-message "socket created here" } */
73 memset (&addr
, 0, sizeof (addr
));
74 addr
.sun_family
= AF_UNIX
;
75 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
76 bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
)); /* { dg-warning "leak of file descriptor 'fd'" } */
79 void test_listen_without_bind (int type
)
81 int fd
= socket (AF_UNIX
, type
, 0);
84 listen (fd
, 5); /* { dg-warning "'listen' on file descriptor 'fd' in wrong phase" } */
85 /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "msg" { target *-*-* } .-1 } */
89 void test_listen_on_unchecked_bind (int type
, const char *sockname
)
91 struct sockaddr_un addr
;
92 int fd
= socket (AF_UNIX
, type
, 0);
95 memset (&addr
, 0, sizeof (addr
));
96 addr
.sun_family
= AF_UNIX
;
97 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
98 bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
)); /* { dg-message "when 'bind' fails" } */
99 listen (fd
, 5); /* { dg-warning "'listen' on file descriptor 'fd' in wrong phase" "warning" } */
100 /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "msg" { target *-*-* } .-1 } */