Skip to content

fix: replace panicking expect() with map_or() for edge-case file paths#2679

Open
srpatcha wants to merge 1 commit intomozilla:mainfrom
srpatcha:fix/lru-cache-path-panic
Open

fix: replace panicking expect() with map_or() for edge-case file paths#2679
srpatcha wants to merge 1 commit intomozilla:mainfrom
srpatcha:fix/lru-cache-path-panic

Conversation

@srpatcha
Copy link
Copy Markdown

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.

Path::file_name() returns None for paths ending in '..' which caused
a panic. Build cache tools should handle unusual paths gracefully.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.19%. Comparing base (d11e2e0) to head (9ed2e8a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2679      +/-   ##
==========================================
+ Coverage   74.17%   74.19%   +0.01%     
==========================================
  Files          70       70              
  Lines       39207    39206       -1     
==========================================
+ Hits        29083    29087       +4     
+ Misses      10124    10119       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants