Skip to content

Commit 033fc76

Browse files
committed
Remove uninit_slice_as_mut_ptr.
1 parent b7df3bc commit 033fc76

10 files changed

+26
-28
lines changed

src/bsd_arandom.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3434
// getrandom(2) was introduced in FreeBSD 12.0 and NetBSD 10.0
3535
#[cfg(target_os = "freebsd")]
3636
{
37-
use crate::{util::uninit_slice_as_mut_ptr, util_libc::Weak};
37+
use crate::util_libc::Weak;
3838
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
3939
type GetRandomFn =
4040
unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
4141

4242
if let Some(fptr) = GETRANDOM.ptr() {
4343
let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
4444
return sys_fill_exact(dest, |buf| unsafe {
45-
func(uninit_slice_as_mut_ptr(buf), buf.len(), 0)
45+
func(buf.as_mut_ptr() as *mut u8, buf.len(), 0)
4646
});
4747
}
4848
}

src/fuchsia.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for Fuchsia Zircon
10-
use crate::{util::uninit_slice_as_mut_ptr, Error};
10+
use crate::Error;
1111
use core::mem::MaybeUninit;
1212

1313
#[link(name = "zircon")]
@@ -16,6 +16,6 @@ extern "C" {
1616
}
1717

1818
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
19-
unsafe { zx_cprng_draw(uninit_slice_as_mut_ptr(dest), dest.len()) }
19+
unsafe { zx_cprng_draw(dest.as_mut_ptr() as *mut u8, dest.len()) }
2020
Ok(())
2121
}

src/ios.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for iOS
10-
use crate::{util::uninit_slice_as_mut_ptr, Error};
10+
use crate::Error;
1111
use core::{ffi::c_void, mem::MaybeUninit, ptr::null};
1212

1313
#[link(name = "Security", kind = "framework")]
@@ -17,7 +17,7 @@ extern "C" {
1717

1818
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1919
// Apple's documentation guarantees kSecRandomDefault is a synonym for NULL.
20-
let ret = unsafe { SecRandomCopyBytes(null(), dest.len(), uninit_slice_as_mut_ptr(dest)) };
20+
let ret = unsafe { SecRandomCopyBytes(null(), dest.len(), dest.as_mut_ptr() as *mut u8) };
2121
// errSecSuccess (from SecBase.h) is always zero.
2222
if ret != 0 {
2323
Err(Error::IOS_SEC_RANDOM)

src/js.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8-
use crate::{
9-
util::{uninit_slice_as_mut_ptr, uninit_slice_fill_zero},
10-
Error,
11-
};
8+
use crate::{util::uninit_slice_fill_zero, Error};
129

1310
extern crate std;
1411
use std::{mem::MaybeUninit, thread_local};
@@ -58,7 +55,7 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
5855
}
5956

6057
// SAFETY: `sub_buf`'s length is the same length as `chunk`
61-
unsafe { sub_buf.raw_copy_to_ptr(uninit_slice_as_mut_ptr(chunk)) };
58+
unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr() as *mut u8) };
6259
}
6360
}
6461
};

src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ pub use crate::error::Error;
205205
//
206206
// These should all provide getrandom_inner with the signature
207207
// `fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>`.
208+
// The function MUST fully initialize `dest` when `Ok(())` is returned.
209+
// The function MUST NOT ever write uninitialized bytes into `dest`,
210+
// regardless of what value it returns.
208211
cfg_if! {
209212
if #[cfg(any(target_os = "emscripten", target_os = "haiku",
210213
target_os = "redox"))] {
@@ -290,8 +293,11 @@ cfg_if! {
290293
/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html).
291294
#[inline]
292295
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
293-
// SAFETY: The `&mut MaybeUninit<_>` reference doesn't escape.
294-
getrandom_uninit_slice(unsafe { slice_as_uninit_mut(dest) }).map(|_| ())
296+
// SAFETY: The `&mut MaybeUninit<_>` reference doesn't escape, and
297+
// `getrandom_uninit_slice` guarantees it will never de-initialize any
298+
// part of `dest`.
299+
getrandom_uninit_slice(unsafe { slice_as_uninit_mut(dest) })?;
300+
Ok(())
295301
}
296302

297303
/// Version of the `getrandom` function which fills `dest` with random bytes
@@ -302,6 +308,9 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
302308
/// In other words, it's safe to assume that `dest` is initialized after
303309
/// this function has returned `Ok`.
304310
///
311+
/// No part of `dest` will ever be de-initialized at any point, regardless
312+
/// of what is returned.
313+
///
305314
/// # Examples
306315
///
307316
/// ```ignore

src/macos.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//! Implementation for macOS
1010
use crate::{
1111
use_file,
12-
util::uninit_slice_as_mut_ptr,
1312
util_libc::{last_os_error, Weak},
1413
Error,
1514
};
@@ -23,7 +22,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2322
if let Some(fptr) = GETENTROPY.ptr() {
2423
let func: GetEntropyFn = unsafe { mem::transmute(fptr) };
2524
for chunk in dest.chunks_mut(256) {
26-
let ret = unsafe { func(uninit_slice_as_mut_ptr(chunk), chunk.len()) };
25+
let ret = unsafe { func(chunk.as_mut_ptr() as *mut u8, chunk.len()) };
2726
if ret != 0 {
2827
return Err(last_os_error());
2928
}

src/solid.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
// except according to those terms.
88

99
//! Implementation for SOLID
10-
use crate::{util::uninit_slice_as_mut_ptr, Error};
10+
use crate::Error;
1111
use core::{mem::MaybeUninit, num::NonZeroU32};
1212

1313
extern "C" {
1414
pub fn SOLID_RNG_SampleRandomBytes(buffer: *mut u8, length: usize) -> i32;
1515
}
1616

1717
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
18-
let ret = unsafe { SOLID_RNG_SampleRandomBytes(uninit_slice_as_mut_ptr(dest), dest.len()) };
18+
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr() as *mut u8, dest.len()) };
1919
if ret >= 0 {
2020
Ok(())
2121
} else {

src/util.rs

-7
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ pub unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T]
7575
mem::transmute(slice)
7676
}
7777

78-
/// Polyfill for the unstable `maybe_uninit_slice` feature's
79-
/// `MaybeUninit::slice_as_mut_ptr`.
80-
#[inline(always)]
81-
pub fn uninit_slice_as_mut_ptr<T>(slice: &mut [MaybeUninit<T>]) -> *mut T {
82-
slice.as_mut_ptr() as *mut T
83-
}
84-
8578
#[inline]
8679
pub fn uninit_slice_fill_zero(slice: &mut [MaybeUninit<u8>]) -> &mut [u8] {
8780
slice.iter_mut().for_each(|b| *b = MaybeUninit::zeroed());

src/vxworks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for VxWorks
10-
use crate::{util::uninit_slice_as_mut_ptr, util_libc::last_os_error, Error};
10+
use crate::{util_libc::last_os_error, Error};
1111
use core::{
1212
mem::MaybeUninit,
1313
sync::atomic::{AtomicBool, Ordering::Relaxed},
@@ -28,7 +28,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2828

2929
// Prevent overflow of i32
3030
for chunk in dest.chunks_mut(i32::max_value() as usize) {
31-
let ret = unsafe { libc::randABytes(uninit_slice_as_mut_ptr(chunk), chunk.len() as i32) };
31+
let ret = unsafe { libc::randABytes(chunk.as_mut_ptr() as *mut u8, chunk.len() as i32) };
3232
if ret != 0 {
3333
return Err(last_os_error());
3434
}

src/windows.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use crate::{util::uninit_slice_as_mut_ptr, Error};
9+
use crate::Error;
1010
use core::{ffi::c_void, mem::MaybeUninit, num::NonZeroU32, ptr};
1111

1212
const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002;
@@ -28,7 +28,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2828
let ret = unsafe {
2929
BCryptGenRandom(
3030
ptr::null_mut(),
31-
uninit_slice_as_mut_ptr(chunk),
31+
chunk.as_mut_ptr() as *mut u8,
3232
chunk.len() as u32,
3333
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
3434
)

0 commit comments

Comments
 (0)