| 1 |
/* { dg-require-effective-target sockets } */ |
| 2 |
/* { dg-skip-if "" { powerpc*-*-aix* } } */ |
| 3 |
|
| 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" |
| 10 |
|
| 11 |
void test_bind (int fd, const char *sockname) |
| 12 |
{ |
| 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" } */ |
| 19 |
else |
| 20 |
__analyzer_dump_path (); /* { dg-message "path" } */ |
| 21 |
} |
| 22 |
|
| 23 |
void test_null_bind (int fd) |
| 24 |
{ |
| 25 |
errno = 0; |
| 26 |
int result = bind (fd, NULL, 0); |
| 27 |
__analyzer_eval (result == -1); /* { dg-warning "TRUE" } */ |
| 28 |
__analyzer_eval (errno > 0); /* { dg-warning "TRUE" } */ |
| 29 |
} |
| 30 |
|
| 31 |
void test_double_bind (int fd, const char *sockname) |
| 32 |
{ |
| 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 } */ |
| 40 |
} |
| 41 |
|
| 42 |
int test_uninit_addr (int fd, const char *sockname) |
| 43 |
{ |
| 44 |
struct sockaddr_un addr; |
| 45 |
return bind (fd, (struct sockaddr *)&addr, sizeof (addr)); |
| 46 |
// TODO: complain about uninit addr. |
| 47 |
} |
| 48 |
|
| 49 |
void test_bind_after_connect (int fd, const char *sockname, |
| 50 |
const struct sockaddr *caddr, socklen_t caddrlen) |
| 51 |
{ |
| 52 |
if (connect (fd, caddr, caddrlen) == -1) |
| 53 |
return; |
| 54 |
|
| 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 |
| 61 |
in the stop state. */ |
| 62 |
} |
| 63 |
|
| 64 |
void test_bind_after_accept (int fd, const char *sockname) |
| 65 |
{ |
| 66 |
int afd = accept (fd, NULL, NULL); |
| 67 |
if (afd == -1) |
| 68 |
return; |
| 69 |
|
| 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 } */ |
| 76 |
|
| 77 |
close (afd); |
| 78 |
} |
| 79 |
|
| 80 |
int test_bind_on_constant () |
| 81 |
{ |
| 82 |
return bind (0, NULL, 0); |
| 83 |
} |