[libc-commits] [libc] [libc] refactor expected to correctly destruct payload (PR #195873)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Wed May 6 07:28:46 PDT 2026


SchrodingerZhu wrote:

As for the FD problem, it is already a common pattern inside llvm libc and I believe there is a lot more to have:

FD Object Wrappers

  - libc/src/__support/OSUtil/linux/sysinfo.h:53: ProcParser opens via linux_syscalls::open, stores fd, closes in ~ProcParser(). This is the strict RAII case.
  - libc/src/__support/File/linux/file.cpp:67: openfile() opens with SYS_open/SYS_openat, wraps in LinuxFile; closed by explicit fclose/File::close() through libc/src/__support/File/linux/
    file.cpp:56.
  - libc/src/__support/File/linux/file.cpp:127: create_file_from_fd() adopts an existing fd into LinuxFile; same explicit close path.
  - libc/src/__support/File/linux/dir.cpp:20: platform_opendir() opens with SYS_open/SYS_openat, then libc/src/__support/File/dir.cpp:20 wraps it; closed by explicit closedir/Dir::close().

  Scoped Open/Close Sites

  - libc/src/__support/OSUtil/linux/auxv.h:118: opens /proc/self/auxv, reads it, closes with SYS_close at line 141.
  - libc/src/__support/threads/linux/thread.cpp:440: Thread::set_name opens /proc/.../comm, writes, closes.
  - libc/src/__support/threads/linux/thread.cpp:482: Thread::get_name opens /proc/.../comm, reads, closes.
  - libc/src/spawn/linux/posix_spawn.cpp:43: child-side file-action open; if it must dup2, it closes the temporary fd at line 103. Otherwise the opened fd is intentionally left as the requested
    child fd.

In bionic, they have sth like:    
https://github.com/aosp-mirror/platform_bionic/blob/main/libc/private/ScopedFd.h

I think we can wrap directly around `open` and return `ErrorOr<ScopedFD>` which would be a nice utility to have.

https://github.com/llvm/llvm-project/pull/195873


More information about the libc-commits mailing list