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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by David Malcolm <[email protected]>:

https://gcc.gnu.org/g:8090c102e167c8279440563827108b359b90fc13

commit r17-6663-g8090c102e167c8279440563827108b359b90fc13
Author: Xuanyu Chen <[email protected]>
Date:   Tue Nov 24 21:52:01 2026 +0800

    analyzer: handle closefrom [PR106283]

    Teach the analyzer about closefrom to avoid false file descriptor leak
    reports.  Add a known_function subclass kf_closefrom in sm-fd.cc so
    that the analyzer recognizes both the void-returning (glibc / FreeBSD)
    and int-returning (NetBSD / OpenBSD) variants of closefrom (int lowfd),
    rejecting incompatible prototypes with the same name.

    In impl_call_pre, initialize any return value with defaults and
    refresh errno with an unconstrained conjured value.  In impl_call_post,
    close tracked open descriptors proven to be >= lowfd with lowfd >= 0
    (or any valid fd when lowfd == 0), leave descriptors proven < lowfd
    unchanged, and conservatively move uncertain cases to m_stop to avoid
    false leak and double-close warnings.

    Three socket states (m_new_unknown_socket, m_bound_stream_socket,
    m_listening_stream_socket) are shared between successful calls and
    speculative transitions from m_start on failure paths; they are
    conservatively moved to m_stop here, accepting some precision loss on
    double-close diagnostics to avoid falsely pruning error paths.  For
    int-returning closefrom, the return value is only set to 0 when at
    least one genuinely open descriptor is proved to be closed; this
    single-path model ignores EINTR and thus prunes failure-cleanup paths
    for such calls.

            PR analyzer/106283

    gcc/analyzer/ChangeLog:

            * sm-fd.cc (class kf_closefrom): New.
            (register_known_fd_functions): Register "closefrom".

    gcc/ChangeLog:

            * doc/invoke.texi: Add closefrom to the list of file descriptor
            functions recognized by -fanalyzer.

    gcc/testsuite/ChangeLog:

            * gcc.dg/analyzer/fd-closefrom-1.c: New test.
            * gcc.dg/analyzer/fd-closefrom-2.c: New test.
            * gcc.dg/analyzer/fd-closefrom-3.c: New test.
            * gcc.dg/analyzer/fd-closefrom-4.c: New test.
            * gcc.dg/analyzer/fd-closefrom-5.c: New test.

    Signed-off-by: Xuanyu Chen <[email protected]>

Reply via email to