https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99716

            Bug ID: 99716
           Summary: -Wanalyzer-double-fclose when fclose is called inside
                    a loop
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: rjones at redhat dot com
  Target Milestone: ---

This seems pretty bogus to me:

#include <stdio.h>
#include <stdlib.h>

int
main ()
{
  int i;

  for (i = 0; i < 2; ++i) {
    FILE *fp = fopen ("/tmp/test", "w");
    fprintf (fp, "hello");
    fclose (fp);
  }
  exit (EXIT_SUCCESS);
}

$ gcc -O2 -fanalyzer /tmp/fclose-loop.c -o /tmp/fclose-loop
/tmp/fclose-loop.c: In function ‘main’:
/tmp/fclose-loop.c:12:5: warning: double ‘fclose’ of FILE ‘fp’
[-Wanalyzer-double-fclose]
   12 |     fclose (fp);
      |     ^~~~~~~~~~~
  ‘main’: events 1-7
    |
    |    9 |   for (i = 0; i < 2; ++i) {
    |      |               ~~^~~
    |      |                 |
    |      |                 (1) following ‘true’ branch (when ‘i != 2’)...
    |      |                 (5) following ‘true’ branch (when ‘i != 2’)...
    |   10 |     FILE *fp = fopen ("/tmp/test", "w");
    |      |                ~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                |
    |      |                (2) ...to here
    |      |                (3) opened here
    |      |                (6) ...to here
    |   11 |     fprintf (fp, "hello");
    |   12 |     fclose (fp);
    |      |     ~~~~~~~~~~~  
    |      |     |
    |      |     (4) first ‘fclose’ here
    |      |     (7) second ‘fclose’ here; first ‘fclose’ was at (4)

This is with:
gcc-11.0.0-0.19.fc35.x86_64

I do not have any later version of GCC to test, but also I don't see any
other bug about this.

Reply via email to