Skip to content

Commit ccc4774

Browse files
elichaijosephlr
authored andcommitted
Handle zero-length slices (#104)
Ensure and document that we do nothing when an empty slice is passed. Note that this changes are for consistency, not to prevent UB.
1 parent 5cfa668 commit ccc4774

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,18 @@ cfg_if! {
258258
/// source.
259259
///
260260
/// This function returns an error on any failure, including partial reads. We
261-
/// make no guarantees regarding the contents of `dest` on error.
261+
/// make no guarantees regarding the contents of `dest` on error. If `dest` is
262+
/// empty, `getrandom` immediately returns success, making no calls to the
263+
/// underlying operating system.
262264
///
263265
/// Blocking is possible, at least during early boot; see module documentation.
264266
///
265267
/// In general, `getrandom` will be fast enough for interactive usage, though
266268
/// significantly slower than a user-space CSPRNG; for the latter consider
267269
/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html).
268270
pub fn getrandom(dest: &mut [u8]) -> Result<(), error::Error> {
271+
if dest.is_empty() {
272+
return Ok(())
273+
}
269274
imp::getrandom_inner(dest)
270275
}

0 commit comments

Comments
 (0)