(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
fd-4.c
       1  #ifdef _AIX
       2  #define _MODE_T
       3  #endif
       4  #include <stdio.h>
       5  
       6  int open(const char *, int mode);
       7  void close(int fd);
       8  int write (int fd, void *buf, int nbytes);
       9  int read (int fd, void *buf, int nbytes);
      10  
      11  #define O_ACCMODE 0xf
      12  #define O_RDONLY 0
      13  #define O_WRONLY 1
      14  #define O_RDWR 2
      15  
      16  typedef enum {
      17    S_IRWXU
      18    // etc
      19  } mode_t;
      20  
      21  int creat (const char *, mode_t mode);
      22  
      23  void
      24  test_1 (const char *path, void *buf)
      25  {
      26      int fd = open (path, O_RDONLY); /* { dg-message "opened here as read-only" } */
      27      if (fd >= 0) /* { dg-message "assuming 'fd' is a valid file descriptor \\(>= 0\\)" "event1" } */
      28      /* { dg-message "following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */
      29      {
      30          write (fd, buf, 1); /* { dg-warning "'write' on read-only file descriptor 'fd'" "warning" } */
      31          /* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */
      32          /* { dg-message "\\(5\\) 'write' on read-only file descriptor 'fd'" "event2" { target *-*-* } .-2 } */
      33          close (fd);
      34      }
      35  }
      36  
      37  void
      38  test_2 (const char *path, void *buf)
      39  {
      40      int fd = open (path, O_WRONLY); /* { dg-message "opened here as write-only" } */
      41      if (fd >= 0) /* { dg-message "assuming 'fd' is a valid file descriptor \\(>= 0\\)" "event1" } */
      42      /* { dg-message "following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */
      43      {
      44          read (fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd'" "warning" } */
      45          /* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */
      46          /* { dg-message "\\(5\\) 'read' on write-only file descriptor 'fd'" "event2" { target *-*-* } .-2 } */
      47          close (fd);
      48      }
      49  }
      50  
      51  
      52  void 
      53  test_3 (const char *path, void *buf)
      54  {
      55      int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
      56      if (fd >= 0)
      57      {
      58          close(fd); /* {dg-message "\\(2\\) closed here"} */
      59          read(fd, buf, 1); /* { dg-warning "'read' on closed file descriptor 'fd'" }  */
      60          /* {dg-message "\\(3\\) 'read' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" {target *-*-*} .-1 } */
      61      }
      62  }
      63  
      64  void 
      65  test_4 (const char *path, void *buf)
      66  {
      67      int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
      68      if (fd >= 0)
      69      {
      70          close(fd); /* {dg-message "\\(2\\) closed here"} */
      71          write(fd, buf, 1); /* { dg-warning "'write' on closed file descriptor 'fd'" }  */
      72          /* {dg-message "\\(3\\) 'write' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" {target *-*-*} .-1 } */
      73      }
      74  }
      75  
      76  void
      77  test_5 (const char *path)
      78  {
      79      int fd = open (path, O_RDWR);
      80      close(fd);
      81      printf("%d", fd); /* { dg-bogus "'printf' on a closed file descriptor 'fd'" } */
      82  }
      83  
      84  
      85  void
      86  test_6 (const char *path, mode_t mode, void *buf)
      87  {
      88    int fd = creat (path, mode);
      89    if (fd != -1)
      90    {
      91      read (fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd'" } */
      92      close(fd);
      93    }
      94  }
      95  
      96  void
      97  test_7 (const char *path, mode_t mode, void *buf)
      98  {
      99    int fd = creat (path, mode);
     100    if (fd != -1)
     101    {
     102      write (fd, buf, 1);
     103      close(fd);
     104    }
     105  }