Skip to content

lscpu: handle incomplete sysfs without panicking - #720

Open
tuxnode wants to merge 1 commit into
uutils:mainfrom
tuxnode:lscpu/no-panic
Open

tuxnode wants to merge 1 commit into
uutils:mainfrom
tuxnode:lscpu/no-panic

Conversation

@tuxnode

@tuxnode tuxnode commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Summary

#717

lscpu still panics when sysfs is incomplete, for example when cache files or topology files are missing (#494). Treat missing or malformed data as partial information instead.

Changes

  • Fall back to an unknown package ID (-1) when physical_package_id is missing.
  • Treat a CPU without core_id as its own core.
  • Skip cache entries with missing/invalid type, level, size or shared_cpu_map.
  • Make CacheSize::parse and parse_cpu_list fallible instead of panicking.
  • Handle non-UTF-8 vulnerability filenames without slicing.

Tests

Added fixture-based tests for:

  • missing cache directory
  • missing/invalid cache metadata
  • unknown cache type
  • missing topology files
  • invalid CPU list

Treat missing or malformed topology and cache metadata as partial data instead of unwrapping or panicking. Skip unreadable cache entries, parse CPU lists and cache sizes fallibly, and add fixture-based tests for missing cache directories, cache metadata, topology files and invalid CPU lists.
Comment thread src/uu/lscpu/src/sysfs.rs
.unwrap();
let core_id = read_trimmed(&cpu_dir.join("topology/core_id"))
.and_then(|value| value.parse::<usize>().ok())
.unwrap_or(cpu_index);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core_count dedups core_id globally, so if only some cpus lack core_id, the cpu index can match a real core_id, no?
could you please add a test with only one cpu missing it?

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