Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace as casts with safer conversions #510

Merged
merged 1 commit into from
Oct 10, 2024
Merged

Replace as casts with safer conversions #510

merged 1 commit into from
Oct 10, 2024

Conversation

newpavlov
Copy link
Member

Based on #445.

const _: () =
assert!(core::mem::size_of::<libc::c_long>() == core::mem::size_of::<libc::ssize_t>());
res.try_into()
.expect("c_long to ssize_t conversion is lossless")
Copy link
Member Author

@newpavlov newpavlov Oct 9, 2024

Choose a reason for hiding this comment

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

This cast will be no longer needed if #508 lands.

@newpavlov newpavlov changed the title Replace more casts with safer conversions Replace as casts with safer conversions Oct 9, 2024
@@ -24,10 +24,16 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// https://docs.rs/wasi/0.11.0+wasi-snapshot-preview1/src/wasi/lib_generated.rs.html#2046-2062
// Note that size of an allocated object can not be bigger than isize::MAX bytes.
// WASI 0.1 supports only 32-bit WASM, so casting length to `i32` is safe.
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
Copy link
Member Author

@newpavlov newpavlov Oct 9, 2024

Choose a reason for hiding this comment

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

This is the only place for which we have to disable the lints. Unfortunately, the compiler does not use the requirement that non-ZST allocations can not be bigger than isize::MAX during optimizations, so using i32::try_from(dest.len()).unwrap() will contain a panic. We could return Error::UNEXPECTED if dest.len() > i32::MAX, but I don't like such hack.

Also, IIRC WASI executors interprets i32 length in random_get as an unsigned integer, so we could've legitimately used cast_signed here, but unfortunately it's not stable.

@newpavlov newpavlov merged commit 09145a6 into master Oct 10, 2024
52 checks passed
@newpavlov newpavlov deleted the as_casts branch October 10, 2024 02:08
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.

1 participant