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 via email to