(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
fd-symbolic-socket.c
       1  /* { dg-require-effective-target sockets } */
       2  /* { dg-skip-if "" { powerpc*-*-aix* } } */
       3  
       4  #include <sys/socket.h>
       5  #include <sys/un.h>
       6  #include <unistd.h>
       7  #include <errno.h>
       8  #include "analyzer-decls.h"
       9  
      10  void test_leak_socket (int type)
      11  {
      12    int fd = socket (AF_UNIX, type, 0); /* { dg-message "socket created here" } */
      13  } /* { dg-warning "leak of file descriptor 'fd'" } */
      14  
      15  void test_leak_socket_no_lhs (int type)
      16  {
      17    socket (AF_UNIX, type, 0);  /* { dg-warning "leak of file descriptor" } */
      18  }
      19  
      20  void test_close_unchecked_socket (int type)
      21  {
      22    int fd = socket (AF_UNIX, type, 0);
      23    close (fd);
      24  }
      25  
      26  void test_close_checked_socket (int type)
      27  {
      28    int fd = socket (AF_UNIX, type, 0);
      29    if (fd == -1)
      30      return;
      31    close (fd);
      32  }
      33  
      34  void test_leak_checked_socket (int type)
      35  {
      36    int fd = socket (AF_UNIX, type, 0); /* { dg-message "socket created here" } */
      37    if (fd == -1) /* { dg-warning "leak of file descriptor 'fd'" } */
      38      return;
      39    // TODO: strange location for leak message
      40  }
      41  
      42  void test_bind_on_checked_socket (int type, const char *sockname)
      43  {
      44    struct sockaddr_un addr;
      45    int fd = socket (AF_UNIX, type, 0);
      46    if (fd == -1)
      47      return;
      48    memset (&addr, 0, sizeof (addr));
      49    addr.sun_family = AF_UNIX;
      50    strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
      51    bind (fd, (struct sockaddr *)&addr, sizeof (addr));
      52    close (fd);
      53  }
      54  
      55  void test_bind_on_unchecked_socket (int type, const char *sockname)
      56  {
      57    struct sockaddr_un addr;
      58    int fd = socket (AF_UNIX, type, 0); /* { dg-message "when 'socket' fails" } */
      59    memset (&addr, 0, sizeof (addr));
      60    addr.sun_family = AF_UNIX;
      61    strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
      62    bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-warning "'bind' on possibly invalid file descriptor 'fd'" } */
      63    close (fd);
      64  }
      65  
      66  void test_leak_of_bound_socket (int type, const char *sockname)
      67  {
      68    struct sockaddr_un addr;
      69    int fd = socket (AF_UNIX, type, 0); /* { dg-message "socket created here" } */
      70    if (fd == -1)
      71      return;
      72    memset (&addr, 0, sizeof (addr));
      73    addr.sun_family = AF_UNIX;
      74    strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
      75    bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-warning "leak of file descriptor 'fd'" } */
      76  }
      77  
      78  void test_listen_without_bind (int type)
      79  {
      80    int fd = socket (AF_UNIX, type, 0);
      81    if (fd == -1)
      82      return;
      83    listen (fd, 5); /* { dg-warning "'listen' on file descriptor 'fd' in wrong phase" } */
      84    /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "msg" { target *-*-* } .-1 } */
      85    close (fd);
      86  }
      87  
      88  void test_listen_on_unchecked_bind (int type, const char *sockname)
      89  {
      90    struct sockaddr_un addr;
      91    int fd = socket (AF_UNIX, type, 0);
      92    if (fd == -1)
      93      return;
      94    memset (&addr, 0, sizeof (addr));
      95    addr.sun_family = AF_UNIX;
      96    strncpy (addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
      97    bind (fd, (struct sockaddr *)&addr, sizeof (addr)); /* { dg-message "when 'bind' fails" } */
      98    listen (fd, 5); /* { dg-warning "'listen' on file descriptor 'fd' in wrong phase" "warning" } */
      99    /* { dg-message "'listen' expects a bound stream socket file descriptor but 'fd' has not yet been bound" "msg" { target *-*-* } .-1 } */
     100    close (fd);
     101  }