Skip to content

lscpu: handle incomplete sysfs without panicking - #720

Open
tuxnode wants to merge 3 commits into
uutils:mainfrom
tuxnode:lscpu/no-panic
Open

tuxnode wants to merge 3 commits 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.
  • skip topology-derived fields when the online CPU list is empty

Tests

Added fixture-based tests for:

  • missing cache directory
  • missing/invalid cache metadata
  • unknown cache type
  • missing topology files
  • invalid CPU list
  • empty CPU list, invalid cache metadata/topology values, non-UTF-8 vulnerability names, cross-package core_id unit tests

@tuxnode
tuxnode force-pushed the lscpu/no-panic branch 2 times, most recently from 9741c38 to ce2f8c6 Compare November 28, 2026 08:31
Comment thread src/uu/lscpu/src/sysfs.rs Resolved
@tuxnode

tuxnode commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Done in 7f746a3

@tuxnode
tuxnode requested a review from sylvestre November 29, 2026 08:23
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.
Address review feedback: core_count() deduplicated core_id globally, so a
CPU with a missing core_id (which fell back to its CPU index) could be merged
with an unrelated core that had the same numeric id, undercounting cores.

Store core_id as Option<usize>, count each CPU without one as its own core,
and key known cores by (package, core) since core_id is only unique within a
package. Add a test where only one CPU is missing core_id.
Skip topology-derived fields when the online CPU list is empty, which
previously divided by zero and panicked on incomplete sysfs.

Extend coverage for malformed/partial sysfs:
- missing cache type and invalid cache level/size
- invalid physical_package_id/core_id values
- non-UTF-8 vulnerability filenames
- empty online CPU lists

Add core_count unit tests for same core_id across packages, SMT
deduplication within a package, and mixed missing core ids.
@tuxnode

tuxnode commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up commit 9e84741 fixes a remaining panic on incomplete sysfs:

  • An empty online CPU list no longer divides by zero when computing Thread(s) per core / Core(s) per socket.
  • Adds tests for missing/invalid cache metadata, non-numeric topology IDs, non-UTF-8 vulnerability names, and empty CPU lists, plus core_count edge-case unit tests.

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