Skip to content

Commit 1390386

Browse files
authored
Increase MSRV to 1.38 and use ptr.cast::<T>() whenever practical to clarify casts. (#425)
Avoid anonymous type casts of the form `as *{const,mut} _`. Make it clearer that we're never casting away constness by avoiding `as *mut T` whenever practical.
1 parent bcbadc1 commit 1390386

19 files changed

+39
-27
lines changed

.clippy.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.36"
1+
msrv = "1.38"

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
strategy:
4545
matrix:
4646
os: [ubuntu-22.04, windows-2022]
47-
toolchain: [nightly, beta, stable, 1.36]
47+
toolchain: [nightly, beta, stable, 1.38]
4848
# Only Test macOS on stable to reduce macOS CI jobs
4949
include:
5050
# x86_64-apple-darwin.

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Breaking Changes
10+
- Update MSRV to 1.38 [#425]
11+
12+
[#425]: https://github.com/rust-random/getrandom/pull/425
13+
714
## [0.2.15] - 2024-05-06
815
### Added
916
- Apple visionOS support [#410]

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ crate features, WASM support and Custom RNGs see the
5252

5353
## Minimum Supported Rust Version
5454

55-
This crate requires Rust 1.36.0 or later.
55+
This crate requires Rust 1.38.0 or later.
5656

5757
## Platform Support
5858

src/apple-other.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern "C" {
1414
}
1515

1616
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
17-
let ret = unsafe { CCRandomGenerateBytes(dest.as_mut_ptr() as *mut c_void, dest.len()) };
17+
let ret = unsafe { CCRandomGenerateBytes(dest.as_mut_ptr().cast::<c_void>(), dest.len()) };
1818
// kCCSuccess (from CommonCryptoError.h) is always zero.
1919
if ret != 0 {
2020
Err(Error::IOS_SEC_RANDOM)

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl Error {
9999
cfg_if! {
100100
if #[cfg(unix)] {
101101
fn os_err(errno: i32, buf: &mut [u8]) -> Option<&str> {
102-
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
102+
let buf_ptr = buf.as_mut_ptr().cast::<libc::c_char>();
103103
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
104104
return None;
105105
}

src/fuchsia.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ extern "C" {
88
}
99

1010
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
11-
unsafe { zx_cprng_draw(dest.as_mut_ptr() as *mut u8, dest.len()) }
11+
unsafe { zx_cprng_draw(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
1212
Ok(())
1313
}

src/getentropy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
//!
99
//! For these targets, we use getentropy(2) because getrandom(2) doesn't exist.
1010
use crate::{util_libc::last_os_error, Error};
11-
use core::mem::MaybeUninit;
11+
use core::{ffi::c_void, mem::MaybeUninit};
1212

1313
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1414
for chunk in dest.chunks_mut(256) {
15-
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr() as *mut libc::c_void, chunk.len()) };
15+
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
1616
if ret != 0 {
1717
return Err(last_os_error());
1818
}

src/getrandom.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
//! nothing. On illumos, the default pool is used to implement getentropy(2),
1717
//! so we assume it is acceptable here.
1818
use crate::{util_libc::sys_fill_exact, Error};
19-
use core::mem::MaybeUninit;
19+
use core::{ffi::c_void, mem::MaybeUninit};
2020

2121
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2222
sys_fill_exact(dest, |buf| unsafe {
23-
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
23+
libc::getrandom(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
2424
})
2525
}

src/hermit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern "C" {
1313

1414
pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1515
while !dest.is_empty() {
16-
let res = unsafe { sys_read_entropy(dest.as_mut_ptr() as *mut u8, dest.len(), 0) };
16+
let res = unsafe { sys_read_entropy(dest.as_mut_ptr().cast::<u8>(), dest.len(), 0) };
1717
// Positive `isize`s can be safely casted to `usize`
1818
if res > 0 && (res as usize) <= dest.len() {
1919
dest = &mut dest[res as usize..];

src/js.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
4040
// have a notion of "uninitialized memory", this is purely
4141
// a Rust/C/C++ concept.
4242
let res = n.random_fill_sync(unsafe {
43-
Uint8Array::view_mut_raw(chunk.as_mut_ptr() as *mut u8, chunk.len())
43+
Uint8Array::view_mut_raw(chunk.as_mut_ptr().cast::<u8>(), chunk.len())
4444
});
4545
if res.is_err() {
4646
return Err(Error::NODE_RANDOM_FILL_SYNC);
@@ -60,7 +60,7 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
6060
}
6161

6262
// SAFETY: `sub_buf`'s length is the same length as `chunk`
63-
unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr() as *mut u8) };
63+
unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr().cast::<u8>()) };
6464
}
6565
}
6666
};

src/netbsd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
99
libc::sysctl(
1010
MIB.as_ptr(),
1111
MIB.len() as libc::c_uint,
12-
buf.as_mut_ptr() as *mut _,
12+
buf.as_mut_ptr().cast::<c_void>(),
1313
&mut len,
1414
ptr::null(),
1515
0,
@@ -29,7 +29,7 @@ static GETRANDOM: LazyPtr = LazyPtr::new();
2929

3030
fn dlsym_getrandom() -> *mut c_void {
3131
static NAME: &[u8] = b"getrandom\0";
32-
let name_ptr = NAME.as_ptr() as *const libc::c_char;
32+
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
3333
unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) }
3434
}
3535

@@ -38,7 +38,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3838
if !fptr.is_null() {
3939
let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
4040
return sys_fill_exact(dest, |buf| unsafe {
41-
func(buf.as_mut_ptr() as *mut u8, buf.len(), 0)
41+
func(buf.as_mut_ptr().cast::<u8>(), buf.len(), 0)
4242
});
4343
}
4444

src/solaris.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
//! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2
1414
//! which also explains why this crate should not use getentropy(2).
1515
use crate::{util_libc::last_os_error, Error};
16-
use core::mem::MaybeUninit;
16+
use core::{ffi::c_void, mem::MaybeUninit};
1717

1818
const MAX_BYTES: usize = 1024;
1919

2020
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2121
for chunk in dest.chunks_mut(MAX_BYTES) {
22-
let ptr = chunk.as_mut_ptr() as *mut libc::c_void;
22+
let ptr = chunk.as_mut_ptr().cast::<c_void>();
2323
let ret = unsafe { libc::getrandom(ptr, chunk.len(), libc::GRND_RANDOM) };
2424
// In case the man page has a typo, we also check for negative ret.
2525
if ret <= 0 {

src/solid.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern "C" {
77
}
88

99
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
10-
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr() as *mut u8, dest.len()) };
10+
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr().cast::<u8>(), dest.len()) };
1111
if ret >= 0 {
1212
Ok(())
1313
} else {

src/use_file.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
};
66
use core::{
77
cell::UnsafeCell,
8+
ffi::c_void,
89
mem::MaybeUninit,
910
sync::atomic::{AtomicUsize, Ordering::Relaxed},
1011
};
@@ -21,7 +22,7 @@ const FD_UNINIT: usize = usize::max_value();
2122
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2223
let fd = get_rng_fd()?;
2324
sys_fill_exact(dest, |buf| unsafe {
24-
libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len())
25+
libc::read(fd, buf.as_mut_ptr().cast::<c_void>(), buf.len())
2526
})
2627
}
2728

src/util_libc.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ pub fn sys_fill_exact(
7474
pub unsafe fn open_readonly(path: &str) -> Result<libc::c_int, Error> {
7575
debug_assert_eq!(path.as_bytes().last(), Some(&0));
7676
loop {
77-
let fd = libc::open(path.as_ptr() as *const _, libc::O_RDONLY | libc::O_CLOEXEC);
77+
let fd = libc::open(
78+
path.as_ptr().cast::<libc::c_char>(),
79+
libc::O_RDONLY | libc::O_CLOEXEC,
80+
);
7881
if fd >= 0 {
7982
return Ok(fd);
8083
}
@@ -92,7 +95,7 @@ pub fn getrandom_syscall(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
9295
unsafe {
9396
libc::syscall(
9497
libc::SYS_getrandom,
95-
buf.as_mut_ptr() as *mut libc::c_void,
98+
buf.as_mut_ptr().cast::<core::ffi::c_void>(),
9699
buf.len(),
97100
0,
98101
) as libc::ssize_t

src/vxworks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2020

2121
// Prevent overflow of i32
2222
for chunk in dest.chunks_mut(i32::max_value() as usize) {
23-
let ret = unsafe { libc::randABytes(chunk.as_mut_ptr() as *mut u8, chunk.len() as i32) };
23+
let ret = unsafe { libc::randABytes(chunk.as_mut_ptr().cast::<u8>(), chunk.len() as i32) };
2424
if ret != 0 {
2525
return Err(last_os_error());
2626
}

src/wasi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::{
77
use wasi::random_get;
88

99
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
10-
unsafe { random_get(dest.as_mut_ptr() as *mut u8, dest.len()) }.map_err(|e| {
10+
unsafe { random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) }.map_err(|e| {
1111
// The WASI errno will always be non-zero, but we check just in case.
1212
match NonZeroU16::new(e.raw()) {
1313
Some(r) => Error::from(NonZeroU32::from(r)),

src/windows.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2929
let ret = unsafe {
3030
BCryptGenRandom(
3131
ptr::null_mut(),
32-
chunk.as_mut_ptr() as *mut u8,
32+
chunk.as_mut_ptr().cast::<u8>(),
3333
chunk.len() as u32,
3434
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
3535
)
@@ -39,8 +39,9 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3939
// Failed. Try RtlGenRandom as a fallback.
4040
#[cfg(not(target_vendor = "uwp"))]
4141
{
42-
let ret =
43-
unsafe { RtlGenRandom(chunk.as_mut_ptr() as *mut c_void, chunk.len() as u32) };
42+
let ret = unsafe {
43+
RtlGenRandom(chunk.as_mut_ptr().cast::<c_void>(), chunk.len() as u32)
44+
};
4445
if ret != 0 {
4546
continue;
4647
}

0 commit comments

Comments
 (0)