[libc-commits] [libc] [libc] Use RAII and DirScanResult in scandir (PR #240107)

via libc-commits libc-commits at lists.llvm.org
Sun Nov 8 00:51:26 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Jeff Bailey (kaladron)

<details>
<summary>Changes</summary>

When building LLVM libc in full-build mode, dir_scan_impl_test fails with Scudo due to unaligned buffer casts and raw allocations in the test mock.  In production code, scan_impl relied on manual error jumps to free partially accumulated entries and close the directory descriptor, leaving callers vulnerable to leaks if intermediate failures occurred.

Modernise scan_impl and Dir::scan to manage the directory lifecycle and accumulated entries using RAII (unique_ptr, ScannedEntries, and AllocChecker), eliminating leak hazards on error paths at zero overhead to the happy path.  Return an ErrorOr<DirScanResult> pair rather than mutating an indirect out-pointer, decouple dir.h from the scanning template via dir_scan_result.h, and update unit tests with aligned buffers and span-based loops.

Assisted-by: Automated tooling, human reviewed.

---
Full diff: https://github.com/llvm/llvm-project/pull/240107.diff


10 Files Affected:

- (modified) libc/src/__support/File/CMakeLists.txt (+23-4) 
- (modified) libc/src/__support/File/dir.cpp (+4-5) 
- (modified) libc/src/__support/File/dir.h (+4-4) 
- (modified) libc/src/__support/File/dir_scan_impl.h (+121-73) 
- (added) libc/src/__support/File/dir_scan_result.h (+38) 
- (modified) libc/src/dirent/CMakeLists.txt (+3-2) 
- (modified) libc/src/dirent/scandir.cpp (+11-4) 
- (modified) libc/test/src/__support/File/CMakeLists.txt (+4-1) 
- (modified) libc/test/src/__support/File/dir_scan_impl_test.cpp (+202-149) 
- (modified) libc/test/src/dirent/scandir_test.cpp (+14) 


``````````diff
<html><body><h1>504 Gateway Time-out</h1>
The server didn't respond in time.
</body></html>

``````````

</details>


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


More information about the libc-commits mailing list