[Bug analyzer/109839] -Wanalyzer-fd-leak false positive with routine dup2

"dmalcolm at gcc dot gnu.org via Gcc-bugs" <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109839

David Malcolm <dmalcolm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |126830
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2026-08-13
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
Here's another example that Claude Code generated for me:

$ cat t.c
#include <unistd.h>
#include <fcntl.h>

void redirect_stdout(const char *path)
{
  int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd < 0)
    return;

  int newfd = dup2(fd, STDOUT_FILENO);
  if (newfd < 0) {
    close(fd);
    return;
  }
  close(fd);
  /* newfd == STDOUT_FILENO == 1.  Stdout is intentionally left open,
     now redirected to path.  */
}

$ gcc -fanalyzer -c t.c
t.c: In function 'redirect_stdout':
t.c:17:1: warning: leak of file descriptor 'newfd' [-Wanalyzer-fd-leak]

https://godbolt.org/z/3vro6sKWr

Seen in the August 2026 OpenScanHub mass scan of Fedora 45 (PR 126830)
affecting json-c, audit/audispd, bash, and others (est. ~50-70 FPs).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126830
[Bug 126830] Tracker bug for -fanalyzer false positives seen in August 2026
OpenScanHub mass scan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.