https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109839
Bug ID: 109839 Summary: -Wanalyzer-fd-leak false positive with routine dup2 Product: gcc Version: 13.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: analyzer Assignee: dmalcolm at gcc dot gnu.org Reporter: eggert at cs dot ucla.edu Target Milestone: --- Created attachment 55072 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55072&action=edit test program illustrating false positive with -Wanalyzer-fd-leak I ran into this problem when compiling GNU Emacs with GCC 13.1.1 20230426 (Red Hat 13.1.1-1) on x86-64. Compile the attached program (which is a stripped-down version) with: gcc -fanalyzer dup2-analyzer.c The output is as follows. This is a false alarm, as the dup2 is a routine I/O redirection and should not be considered to be a leak. In typical use, it's better to not consider dup2's return value to be a leak. dup2-analyzer.c: In function ‘main’: dup2-analyzer.c:6:6: warning: leak of file descriptor ‘dup2(1, 2)’ [CWE-775] [-Wanalyzer-fd-leak] 6 | if (dup2 (STDOUT_FILENO, STDERR_FILENO) < 0) | ^ ‘main’: events 1-2 | | 6 | if (dup2 (STDOUT_FILENO, STDERR_FILENO) < 0) | | ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | || | | |(1) opened here | | (2) ‘dup2(1, 2)’ leaks here; was opened at (1) |