public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/108246] New: False-positive Wanalyzer-double-fclose on fclose() in loop
@ 2022-12-28 18:57 roman.zilka at gmail dot com
  0 siblings, 0 replies; only message in thread
From: roman.zilka at gmail dot com @ 2022-12-28 18:57 UTC (permalink / raw)
  To: gcc-bugs

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)
    |

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-12-28 18:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-28 18:57 [Bug analyzer/108246] New: False-positive Wanalyzer-double-fclose on fclose() in loop roman.zilka at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).