public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/109839] New: -Wanalyzer-fd-leak false positive with routine dup2
@ 2023-05-13  2:17 eggert at cs dot ucla.edu
  2024-01-11  8:18 ` [Bug analyzer/109839] " buczek at molgen dot mpg.de
  0 siblings, 1 reply; 2+ messages in thread
From: eggert at cs dot ucla.edu @ 2023-05-13  2:17 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug analyzer/109839] -Wanalyzer-fd-leak false positive with routine dup2
  2023-05-13  2:17 [Bug analyzer/109839] New: -Wanalyzer-fd-leak false positive with routine dup2 eggert at cs dot ucla.edu
@ 2024-01-11  8:18 ` buczek at molgen dot mpg.de
  0 siblings, 0 replies; 2+ messages in thread
From: buczek at molgen dot mpg.de @ 2024-01-11  8:18 UTC (permalink / raw)
  To: gcc-bugs

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

Donald Buczek <buczek at molgen dot mpg.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |buczek at molgen dot mpg.de

--- Comment #1 from Donald Buczek <buczek at molgen dot mpg.de> ---
Correct, dup2() does not allocate a new file descriptor, so the returned file
descriptor can't be leaked. 

Reduced test case:

    #include <unistd.h>
    void f(int old, int new) {
        int r = dup2(old, new);
    }


https://gcc.godbolt.org/z/6sP9v1r5a

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-11  8:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-13  2:17 [Bug analyzer/109839] New: -Wanalyzer-fd-leak false positive with routine dup2 eggert at cs dot ucla.edu
2024-01-11  8:18 ` [Bug analyzer/109839] " buczek at molgen dot mpg.de

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