1  /* Various operations done on sockets in the wrong phase.  */
       2  
       3  /* { dg-require-effective-target sockets } */
       4  /* { dg-skip-if "" { powerpc*-*-aix* } } */
       5  
       6  #include <sys/types.h>
       7  #include <sys/stat.h>
       8  #include <fcntl.h>
       9  #include <sys/socket.h>
      10  #include <sys/un.h>
      11  #include <unistd.h>
      12  #include <errno.h>
      13  #include "analyzer-decls.h"
      14  
      15  void test_read_on_new_socket (void *buf)
      16  {
      17    int fd = socket (AF_UNIX, SOCK_STREAM, 0); /* { dg-message "stream socket created here" } */
      18    if (fd == -1)
      19      return;
      20    read (fd, buf, 1); /* { dg-warning "'read' on file descriptor 'fd' in wrong phase \\\[-Wanalyzer-fd-phase-mismatch\\\]" "warning" } */
      21    /* { dg-message "'read' expects a stream socket to be connected via 'accept' but 'fd' has not yet been bound" "final event" { target *-*-* } .-1 } */
      22    close (fd);
      23  }
      24  
      25  void test_read_on_bound_socket (int fd, const char *sockname, void *buf)
      26  {
      27    struct sockaddr_un addr;
      28    memset (&addr, 0, sizeof (addr));
      29    addr.sun_family = AF_UNIX;
      30    strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
      31    if (bind (fd, (struct sockaddr *)&addr, sizeof (addr)) == -1)
      32      return;
      33    /* This could be a datagram socket, so we shouldn't complain here.  */
      34    read (fd, buf, 1);
      35  }
      36  
      37  void test_read_on_listening_socket (int fd, void *buf)
      38  {
      39    if (listen (fd, 5) == -1) /* { dg-message "stream socket marked as passive here via 'listen'" } */
      40      return;
      41    read (fd, buf, 1); /* { dg-message "'read' on file descriptor 'fd' in wrong phase" "warning" } */
      42    /* { dg-message "'read' expects a stream socket to be connected via the return value of 'accept' but 'fd' is listening; wrong file descriptor\\\?" "final event" { target *-*-* } .-1 } */
      43  }
      44  
      45  void test_bind_on_non_socket (const char *filename, const char *sockname)
      46  {
      47    int fd = open (filename, O_RDONLY);
      48    if (fd == -1)
      49      return;
      50  
      51    struct sockaddr_un addr;
      52    memset (&addr, 0, sizeof (addr));
      53    addr.sun_family = AF_UNIX;
      54    strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
      55    int result = bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-warning "'bind' on non-socket file descriptor 'fd' \\\[-Wanalyzer-fd-type-mismatch\\\]" "warning" } */
      56    /* { dg-message "'bind' expects a socket file descriptor but 'fd' is not a socket" "final event" { target *-*-* } .-1 } */
      57    __analyzer_eval (result == -1); /* { dg-warning "TRUE" } */
      58    
      59    close (fd);
      60  }
      61  
      62  void test_passive_open_read_on_wrong_socket (int sfd)
      63  {
      64    int cfd = accept (sfd, NULL, NULL);
      65    write (sfd, "hello", 6); /* { dg-warning "'write' on file descriptor 'sfd' in wrong phase" "warning" } */
      66    /* { dg-message "'write' expects a stream socket to be connected via the return value of 'accept' but 'sfd' is listening; wrong file descriptor\\\?" "final event" { target *-*-* } .-1 } */
      67    close (cfd);
      68  }
      69  
      70  void test_listen_on_new_stream_socket (void)
      71  {
      72    int fd = socket (AF_UNIX, SOCK_STREAM, 0);
      73    if (fd == -1)
      74      return;
      75    listen (fd, 5); /* { dg-message "'listen' on file descriptor 'fd' in wrong phase" "warning" } */
      76    /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "final event" { target *-*-* } .-1 } */
      77    close (fd);
      78  }
      79  
      80  void test_accept_on_new_stream_socket (void)
      81  {
      82    int fd = socket (AF_UNIX, SOCK_STREAM, 0);
      83    if (fd == -1)
      84      return;
      85    accept (fd, NULL, NULL); /* { dg-message "'accept' on file descriptor 'fd' in wrong phase" "warning" } */
      86    /* { dg-message "'accept' expects a listening stream socket file descriptor but 'fd' has not yet been bound" "final event" { target *-*-* } .-1 } */
      87    close (fd);
      88  }
      89  
      90  void test_listen_before_bind (int fd, const char *sockname)
      91  {
      92    if (listen (fd, 5) == -1) /* { dg-message "stream socket marked as passive here via 'listen'" } */
      93      return;
      94  
      95    struct sockaddr_un addr;
      96    memset (&addr, 0, sizeof (addr));
      97    addr.sun_family = AF_UNIX;
      98    strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
      99    bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-warning "'bind' on file descriptor 'fd' in wrong phase" "warning" } */
     100    /* { dg-message "'bind' expects a new socket file descriptor but 'fd' is already listening" "final event" { target *-*-* } .-1 } */
     101  }