From 207ac4c9e5d34b24490786b07e0f0cf7bb8a1c2d Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 21 Oct 2022 01:08:35 -0700 Subject: [PATCH] Add back empty slice check https://github.com/rust-random/getrandom/pull/291 inadvertantly removed this check See https://github.com/rust-random/getrandom/pull/104 for why this was added in the first place. Also, per our docs: > If `dest` is empty, `getrandom` immediately returns success, making > no calls to the underlying operating system. Signed-off-by: Joe Richey --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c4809d6b..074a6a7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -322,7 +322,9 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { /// ``` #[inline] pub fn getrandom_uninit(dest: &mut [MaybeUninit]) -> Result<&mut [u8], Error> { - imp::getrandom_inner(dest)?; + if !dest.is_empty() { + imp::getrandom_inner(dest)?; + } // SAFETY: `dest` has been fully initialized by `imp::getrandom_inner` // since it returned `Ok`. Ok(unsafe { slice_assume_init_mut(dest) })