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_bind (int fd
, const char *sockname
)
13 struct sockaddr_un addr
;
14 memset (&addr
, 0, sizeof (addr
));
15 addr
.sun_family
= AF_UNIX
;
16 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
17 if (bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
)) == -1)
18 __analyzer_dump_path (); /* { dg-message "path" } */
20 __analyzer_dump_path (); /* { dg-message "path" } */
23 void test_null_bind (int fd
)
26 int result
= bind (fd
, NULL
, 0);
27 __analyzer_eval (result
== -1); /* { dg-warning "TRUE" } */
28 __analyzer_eval (errno
> 0); /* { dg-warning "TRUE" } */
31 void test_double_bind (int fd
, const char *sockname
)
33 struct sockaddr_un addr
;
34 memset (&addr
, 0, sizeof (addr
));
35 addr
.sun_family
= AF_UNIX
;
36 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
37 bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
));
38 bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
)); /* { dg-warning "'bind' on file descriptor 'fd' in wrong phase \\\[CWE-666\\\] \\\[-Wanalyzer-fd-phase-mismatch\\\]" "warning" } */
39 /* { dg-message "'bind' expects a new socket file descriptor but 'fd' has already been bound" "final event" { target *-*-* } .-1 } */
42 int test_uninit_addr (int fd
, const char *sockname
)
44 struct sockaddr_un addr
;
45 return bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
));
46 // TODO: complain about uninit addr.
49 void test_bind_after_connect (int fd
, const char *sockname
,
50 const struct sockaddr
*caddr
, socklen_t caddrlen
)
52 if (connect (fd
, caddr
, caddrlen
) == -1)
55 struct sockaddr_un addr
;
56 memset (&addr
, 0, sizeof (addr
));
57 addr
.sun_family
= AF_UNIX
;
58 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
59 bind (fd
, (struct sockaddr
*)&addr
, sizeof (addr
));
60 /* TODO: we don't warn for this; after the plain "connect" we're
64 void test_bind_after_accept (int fd
, const char *sockname
)
66 int afd
= accept (fd
, NULL
, NULL
);
70 struct sockaddr_un addr
;
71 memset (&addr
, 0, sizeof (addr
));
72 addr
.sun_family
= AF_UNIX
;
73 strncpy (addr
.sun_path
, sockname
, sizeof(addr
.sun_path
) - 1);
74 bind (afd
, (struct sockaddr
*)&addr
, sizeof (addr
)); /* { dg-warning "'bind' on file descriptor 'afd' in wrong phase \\\[CWE-666\\\] \\\[-Wanalyzer-fd-phase-mismatch\\\]" "warning" } */
75 /* { dg-message "'bind' expects a new socket file descriptor but 'afd' is already connected" "final event" { target *-*-* } .-1 } */
80 int test_bind_on_constant ()
82 return bind (0, NULL
, 0);