(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
file-1.c
       1  typedef struct FILE   FILE;
       2  
       3  FILE* fopen (const char*, const char*);
       4  int   fclose (FILE*);
       5  #define SEEK_SET        0
       6  int fseek (FILE *, long int, int);
       7  
       8  void
       9  test_1 (const char *path)
      10  {
      11    FILE *f = fopen (path, "r"); /* { dg-message "opened here" } */
      12    if (!f)
      13      return;
      14  
      15    fclose (f); /* { dg-message "\\(4\\) \\.\\.\\.to here" "to here" } */
      16    /* { dg-message "\\(5\\) first 'fclose' here" "first fclose" { target *-*-* } .-1 } */
      17    fclose (f); /* { dg-warning "double 'fclose' of FILE 'f' \\\[CWE-1341\\\]" "warning" } */ 
      18    /* { dg-message "second 'fclose' here; first 'fclose' was at \\(5\\)" "second fclose" { target *-*-* } .-1 } */
      19  }
      20  
      21  /* Swallow -Wuse-after-free issued for the same problem
      22     { dg-prune-output "-Wuse-after-free" } */
      23  
      24  void
      25  test_2 (const char *src, const char *dst)
      26  {
      27    FILE *f_in = fopen (src, "r"); /* { dg-message "\\(1\\) opened here" } */
      28    if (!f_in)
      29      return;
      30  
      31    FILE *f_out = fopen (src, "w");
      32    if (!f_out)
      33      return; /* { dg-warning "leak of FILE 'f_in'" "warning" } */
      34    /* { dg-message "\\(7\\) 'f_in' leaks here; was opened at \\(1\\)" "event" { target *-*-* } .-1 } */
      35  
      36    fclose (f_out);
      37    fclose (f_in);
      38  }
      39  
      40  void
      41  test_3 (const char *path)
      42  {
      43    FILE *f = fopen (path, "r"); /* { dg-message "opened here" } */
      44    return; /* { dg-warning "leak of FILE 'f'" } */ 
      45  }
      46  
      47  void
      48  test_4 (const char *path)
      49  {
      50    FILE *f = fopen (path, "r"); /* { dg-message "opened here" } */
      51  
      52    /* Ensure we know about common fns that are known to not close the
      53       file (e.g. "fseek").  */
      54    fseek (f, 1024, SEEK_SET);
      55  
      56    return; /* { dg-warning "leak of FILE 'f'" } */ 
      57  }
      58  
      59  void
      60  test_5 (const char *path)
      61  {
      62    FILE *f = fopen (path, "r"); /* { dg-message "opened here" } */
      63    return; /* { dg-warning "leak of FILE 'f'" } */ 
      64  }