(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
fd-3.c
       1  int open(const char *, int mode);
       2  void close(int fd);
       3  int write (int fd, void *buf, int nbytes);
       4  int read (int fd, void *buf, int nbytes);
       5  int some_condition();
       6  
       7  #define O_RDONLY 0
       8  #define O_WRONLY 1
       9  #define O_RDWR 2
      10  #define STDIN 0
      11  #define O_NOATIME 262144
      12  
      13  void
      14  test_1 (const char *path, void *buf)
      15  {
      16      int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
      17      write (fd, buf, 1); /* { dg-message "\\(2\\) 'fd' could be invalid: unchecked value from \\(1\\)" } */
      18      /* { dg-warning "'write' on possibly invalid file descriptor 'fd'" "warning" { target *-*-* } .-1 } */
      19      close(fd);
      20  }
      21  
      22  void
      23  test_2 (const char *path, void *buf)
      24  {
      25      int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
      26      read (fd, buf, 1); /* { dg-message "\\(2\\) 'fd' could be invalid: unchecked value from \\(1\\)" } */
      27      /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" "warning" { target *-*-* } .-1 } */
      28      close (fd);
      29  }
      30  
      31  void 
      32  test_3 (void *buf)
      33  {
      34      int fd = -1;
      35      read (fd, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" } */
      36      /* { dg-message "\\(1\\) 'fd' could be invalid" "" { target *-*-* } .-1 } */
      37  }
      38  
      39  void
      40  test_4 (void *buf)
      41  {
      42      int fd = STDIN;
      43      read (fd, buf, 1);
      44      close(fd);
      45  }
      46  
      47  void
      48  test_5 (char *path, void *buf)
      49  {
      50      int flags = O_RDONLY;
      51      if (some_condition())
      52          flags |= O_NOATIME;
      53      int fd = open (path, flags); /* { dg-message "\\(1\\) opened here" } */
      54      read (fd, buf, 1); /* { dg-warning "'read' on possibly invalid file descriptor 'fd'" } */
      55      /* { dg-message "\\(2\\) 'fd' could be invalid" "" { target *-*-* } .-1 } */
      56      close (fd);   
      57  }
      58  
      59  
      60  void
      61  test_6 (char *path, void *buf)
      62  {
      63      int fd = open (path, O_RDONLY);
      64      if (fd != -1)
      65      {
      66          read (fd, buf, 1);
      67      }
      68      close (fd);
      69  }
      70  
      71  
      72  void
      73  test_7 (char *path, void *buf)
      74  {
      75      int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
      76      if (fd != -1) /* { dg-message "\\(2\\) assuming 'fd' is an invalid file descriptor \\(< 0\\)" } */
      77      {
      78          read (fd, buf, 1);
      79      } else
      80      {
      81          write (fd, buf, 1); /* { dg-warning "'write' on possibly invalid file descriptor 'fd'" } */
      82          
      83      }
      84      close(fd);
      85  }
      86  
      87  void
      88  test_read_from_symbolic_fd (int fd, void *buf)
      89  {
      90    read (fd, buf, 1);
      91  }
      92  
      93  void
      94  test_write_to_symbolic_fd (int fd, void *buf)
      95  {
      96    write (fd, buf, 1);
      97  }