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

            Bug ID: 108246
           Summary: False-positive Wanalyzer-double-fclose on fclose() in
                    loop
           Product: gcc
           Version: 11.3.1
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: roman.zilka at gmail dot com
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

Created attachment 54164
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54164&action=edit
gcc -v

Encountered a false-positive -Wanalyzer-double-fclose in -fanalyzer in gcc
11.3.1 and 12.2. It's accompanied by -Wanalyzer-double-free, but only in the
11.3.1.

$ cat false_fanalyzer.c 
#include <stdio.h>
#include <errno.h>
int main() {
    FILE *f = fopen("/dev/null", "w");
    if (f) {
        fwrite("asd", 1, 3, f);
        while (fclose(f)) {
            if (errno != EINTR)
                break;
        }
    }
}

$ gcc -fanalyzer -fanalyzer-verbosity=0 false_fanalyzer.c
false_fanalyzer.c: In function ‘main’:
false_fanalyzer.c:7:16: warning: double ‘fclose’ of FILE ‘f’
[-Wanalyzer-double-fclose]
    7 |         while (fclose(f)) {
      |                ^~~~~~~~~
  ‘main’: events 1-4
    |
    |    4 |     FILE *f = fopen("/dev/null", "w");
    |      |               ^~~~~~~~~~~~~~~~~~~~~~~
    |      |               |
    |      |               (1) opened here
    |    5 |     if (f) {
    |      |        ~       
    |      |        |
    |      |        (2) assuming ‘f’ is non-NULL
    |    6 |         fwrite("asd", 1, 3, f);
    |    7 |         while (fclose(f)) {
    |      |                ~~~~~~~~~
    |      |                |
    |      |                (3) first ‘fclose’ here
    |      |                (4) second ‘fclose’ here; first ‘fclose’ was at (3)
    |
false_fanalyzer.c:7:16: warning: double-‘fclose’ of ‘f’ [CWE-415]
[-Wanalyzer-double-free]
    7 |         while (fclose(f)) {
      |                ^~~~~~~~~
  ‘main’: events 1-4
    |
    |    4 |     FILE *f = fopen("/dev/null", "w");
    |      |               ^~~~~~~~~~~~~~~~~~~~~~~
    |      |               |
    |      |               (1) allocated here
    |    5 |     if (f) {
    |      |        ~       
    |      |        |
    |      |        (2) assuming ‘f’ is non-NULL
    |    6 |         fwrite("asd", 1, 3, f);
    |    7 |         while (fclose(f)) {
    |      |                ~~~~~~~~~
    |      |                |
    |      |                (3) first ‘fclose’ here
    |      |                (4) second ‘fclose’ here; first ‘fclose’ was at (3)
    |

Reply via email to