diff --git a/PR_DESCRIPTION.md b/PR_DESCRIPTION.md new file mode 100644 index 000000000..d56ba219d --- /dev/null +++ b/PR_DESCRIPTION.md @@ -0,0 +1,23 @@ +# Fix panic on edge-case file paths in LRU disk cache init + +## Problem + +`LruDiskCache::init()` in `src/lru_disk_cache/mod.rs` calls `.expect("Bad path?")` on `file.file_name()`, which panics if the path ends in `..` or is otherwise unusual. A build cache tool should handle unexpected file system states gracefully rather than crashing. + +## Root Cause + +`Path::file_name()` returns `None` when the path terminates in `..` or consists solely of a root or prefix. The `.expect()` call converts this into a panic. While uncommon, symlinks or filesystem corruption could produce such paths in the cache directory. + +## Fix + +Replaced `.expect("Bad path?").starts_with(TEMPFILE_PREFIX)` with `.map_or(false, |name| name.starts_with(TEMPFILE_PREFIX))`. Paths without a valid file name component are now treated as non-temporary files (skipping the cleanup branch) rather than crashing. + +## Testing + +- Create a cache directory containing a path component ending in `..`. +- Previously: sccache panics during cache init. Now: the entry is handled gracefully. +- Normal cache operation should be unaffected. + +## Impact + +Affects sccache users whose cache directories may contain unusual file paths. A panic in cache init prevents the entire build cache from working. diff --git a/src/lru_disk_cache/mod.rs b/src/lru_disk_cache/mod.rs index 65f744761..174d30ec7 100644 --- a/src/lru_disk_cache/mod.rs +++ b/src/lru_disk_cache/mod.rs @@ -188,8 +188,7 @@ impl LruDiskCache { for (file, size) in get_all_files(&self.root) { if file .file_name() - .expect("Bad path?") - .starts_with(TEMPFILE_PREFIX) + .map_or(false, |name| name.starts_with(TEMPFILE_PREFIX)) { fs::remove_file(&file).unwrap_or_else(|e| { error!("Error removing temporary file `{}`: {}", file.display(), e);