public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "buczek at molgen dot mpg.de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/113329] New: analyzer: False positive analyzer-fd-use-without-check with dup2()
Date: Thu, 11 Jan 2024 08:33:04 +0000	[thread overview]
Message-ID: <bug-113329-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113329
           Summary: analyzer: False positive analyzer-fd-use-without-check
                    with dup2()
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: buczek at molgen dot mpg.de
  Target Milestone: ---

Using a closed file descriptor as the second argument of dup2() is a valid
usage. It makes sense, because the implicit close of dup2() doesn't return
close errors to the caller, so an explicit close might be preferred.

** Code:

#include <unistd.h>
void f(int oldfd, int newfd) {
    close(newfd);
    dup2(oldfd, newfd);
}

** Result:

<source>: In function 'f':
<source>:4:5: warning: 'dup2' on possibly invalid file descriptor 'newfd'
[-Wanalyzer-fd-use-without-check]
    4 |     dup2(oldfd, newfd);
      |     ^~~~~~~~~~~~~~~~~~
  'f': events 1-2
    |
    |    3 |     close(newfd);
    |      |     ^~~~~~~~~~~~
    |      |     |
    |      |     (1) closed here
    |    4 |     dup2(oldfd, newfd);
    |      |     ~~~~~~~~~~~~~~~~~~
    |      |     |
    |      |     (2) 'newfd' could be invalid
    |
Compiler returned: 0

https://gcc.godbolt.org/z/8zac19e15

             reply	other threads:[~2024-01-11  8:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  8:33 buczek at molgen dot mpg.de [this message]
2024-04-28 18:56 ` [Bug analyzer/113329] " alan.coopersmith at oracle dot com
2024-04-28 18:56 ` alan.coopersmith at oracle dot com

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