Skip to content

Handle a negative st_rdev like st_dev - #428

Open
zackees wants to merge 1 commit into
bytecodealliance:mainfrom
zackees:fix/signed-rdev
Open

zackees wants to merge 1 commit into
bytecodealliance:mainfrom
zackees:fix/signed-rdev

Conversation

@zackees

@zackees zackees commented Sep 17, 2026

Copy link
Copy Markdown

Fixes #427.

dev_t is signed on some platforms (on macOS it is i32). from_rustix already handles that for st_dev by sign-extending through i64, but st_rdev has the same type and went through u64::try_from(stat.st_rdev).unwrap(), which panics with TryFromIntError(()) when the device number is negative. We hit this on an x86_64 macOS host: symlink_metadata panics at metadata_ext.rs:171.

This applies the existing st_dev conversion to st_rdev and widens the existing comment to cover both. The function's #[allow(unused_comparisons)] already covers platforms where dev_t is unsigned. Sign-extending matches std: std::os::darwin::fs::MetadataExt::st_rdev returns st_rdev as u64.

There's no regression test, because a negative st_rdev can't be produced portably. The change mirrors the st_dev code a few lines above.

Checked locally with Rust 1.95.0: cargo fmt --all -- --check, cargo test -p cap-primitives (Linux), and cargo check -p cap-primitives for x86_64-apple-darwin and aarch64-apple-darwin.

If this is acceptable, a 4.0.4 patch release would let downstream users pick it up without a [patch]. Thanks!

dev_t is signed on some platforms (macOS: i32), and st_rdev has that type
just like st_dev. st_dev already sign-extends a negative value; st_rdev
went straight through u64::try_from(..).unwrap(), which panics with
TryFromIntError on a negative device number. Apply the same conversion,
which also matches std's MetadataExt::st_rdev (`st_rdev as u64`).
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.

cap-primitives 4.0.3 panics with TryFromIntError on a negative macOS st_rdev

1 participant