Skip to content

Commit

Permalink
Handle zero-length slices (#104)
Browse files Browse the repository at this point in the history
Ensure and document that we do nothing when an empty slice is passed.

Note that this changes are for consistency, not to prevent UB.
  • Loading branch information
elichai authored and josephlr committed Sep 19, 2019
1 parent 5cfa668 commit ccc4774
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,18 @@ cfg_if! {
/// source.
///
/// This function returns an error on any failure, including partial reads. We
/// make no guarantees regarding the contents of `dest` on error.
/// make no guarantees regarding the contents of `dest` on error. If `dest` is
/// empty, `getrandom` immediately returns success, making no calls to the
/// underlying operating system.
///
/// Blocking is possible, at least during early boot; see module documentation.
///
/// In general, `getrandom` will be fast enough for interactive usage, though
/// significantly slower than a user-space CSPRNG; for the latter consider
/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html).
pub fn getrandom(dest: &mut [u8]) -> Result<(), error::Error> {
if dest.is_empty() {
return Ok(())
}
imp::getrandom_inner(dest)
}

0 comments on commit ccc4774

Please sign in to comment.