From 55ad4c41ba5a75e23ed6f2f421f8401e197dcbcd Mon Sep 17 00:00:00 2001 From: Joseph Richey Date: Fri, 21 Oct 2022 07:02:34 -0700 Subject: [PATCH] Add back empty slice check (#298) 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) })