public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "roman.zilka at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/108246] New: False-positive Wanalyzer-double-fclose on fclose() in loop
Date: Wed, 28 Dec 2022 18:57:08 +0000	[thread overview]
Message-ID: <bug-108246-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2022-12-28 18:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108246-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).