Bug 106298 - RFE: analyzer handling of dup, dup2, and dup3
Summary: RFE: analyzer handling of dup, dup2, and dup3
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: analyzer (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Immad Mir
URL:
Keywords:
Depends on:
Blocks: analyzer-fd
  Show dependency treegraph
 
Reported: 2022-07-14 14:42 UTC by David Malcolm
Modified: 2022-08-02 18:03 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-07-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Malcolm 2022-07-14 14:42:15 UTC
Now that -fanalyzer has warnings for file descriptors (especially leaks), we should probably special-case the following functions (rather than attempt to express them via attributes):

 int dup(int oldfd);
 int dup2(int oldfd, int newfd);
 int dup3(int oldfd, int newfd, int flags);

https://man7.org/linux/man-pages/man2/dup.2.html
Comment 1 Eric Gallager 2022-07-14 19:27:31 UTC
Portability issues to be aware of with dup and dup2, per gnulib:
https://www.gnu.org/software/gnulib/manual/html_node/dup.html
https://www.gnu.org/software/gnulib/manual/html_node/dup2.html
Comment 2 David Malcolm 2022-07-27 17:56:32 UTC
Immad's looking at this one.
Comment 3 GCC Commits 2022-08-02 16:54:37 UTC
The master branch has been updated by Immad Mir <mir@gcc.gnu.org>:

https://gcc.gnu.org/g:6a11f2d974a912aaaedb0ce32cdfde10193003cd

commit r13-1936-g6a11f2d974a912aaaedb0ce32cdfde10193003cd
Author: Immad Mir <mirimmad@outlook.com>
Date:   Tue Aug 2 22:22:07 2022 +0530

    analyzer: support for creat, dup, dup2 and dup3 [PR106298]
    
    This patch extends the state machine in sm-fd.cc to support
    creat, dup, dup2 and dup3 functions.
    
    Lightly tested on x86_64 Linux.
    
    gcc/analyzer/ChangeLog:
            PR analyzer/106298
            * sm-fd.cc (fd_state_machine::on_open): Add
            creat, dup, dup2 and dup3 functions.
            (enum dup): New.
            (fd_state_machine::valid_to_unchecked_state): New.
            (fd_state_machine::on_creat): New.
            (fd_state_machine::on_dup): New.
    
    gcc/testsuite/ChangeLog:
            PR analyzer/106298
            * gcc.dg/analyzer/fd-1.c: Add tests for 'creat'.
            * gcc.dg/analyzer/fd-2.c: Likewise.
            * gcc.dg/analyzer/fd-4.c: Likewise.
            * gcc.dg/analyzer/fd-dup-1.c: New tests.
    
    Signed-off-by: Immad Mir <mirimmad@outlook.com>
Comment 4 Immad Mir 2022-08-02 18:03:14 UTC
Analyzer can now recognize these functions. See the commit above.