1  typedef struct FILE   FILE;
       2  
       3  FILE* fopen (const char*, const char*);
       4  int   fclose (FILE*);
       5  char *fgets (char *, int, FILE *);
       6  
       7  #define NULL ((void *)0)
       8  
       9  void f0(const char *str)
      10  {
      11    FILE * fp = fopen(str, "r"); /* { dg-message "opened here" } */
      12    char buf[10];
      13    fgets(buf, 10, fp);
      14  } /* { dg-warning "leak of FILE 'fp'" } */
      15  
      16  void f1(const char *str)
      17  {
      18    FILE * fp = fopen(str, "r"); /* { dg-message "opened here" } */
      19    char buf[10];
      20  
      21    while (fgets(buf, 10, fp) != NULL)
      22      {
      23        /* Do something with buf */
      24      }
      25  } /* { dg-warning "leak of FILE 'fp'" } */
      26  
      27  void f2(const char *str, int flag)
      28  {
      29    FILE * fp = fopen(str, "r"); /* { dg-message "opened here" } */
      30    char buf[10];
      31  
      32    while (fgets(buf, 10, fp) != NULL)
      33      {
      34        /* Do something with buf */
      35      }
      36    if (flag) /* { dg-message "when 'flag == 0'" } */
      37      fclose(fp);
      38  } /* { dg-warning "leak of FILE 'fp'" } */
      39  
      40  extern void called_by_f3( FILE * fp);
      41  
      42  void f3(const char *str)
      43  {
      44    FILE * fp = fopen(str, "r");
      45    char buf[10];
      46  
      47    while (fgets(buf, 10, fp) != NULL)
      48      {
      49        /* Do something with buf */
      50      }
      51    /* Not sure if fclose executed by called_by_f3 or not. Say nothing */
      52    called_by_f3(fp);
      53  }
      54  
      55  void f4(const char *str)
      56  {
      57    FILE * fp = fopen(str, "r");
      58    char buf[10];
      59  
      60    while (fgets(buf, 10, fp) != NULL)
      61      {
      62        /* Do something with buf */
      63      }
      64    /* Nothing to say here. */
      65    fclose(fp);
      66  }
      67  
      68  void main(int argc, const char * argv[])
      69  {
      70    FILE * fp = fopen(argv[0], "r");
      71    char buf[10];
      72  
      73    while (fgets(buf, 10, fp) != NULL)
      74      {
      75        /* Do something with buf */
      76      }
      77    /* Nothing to say here, because we are in main. */
      78  }