Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling to ReseedingRng #32

Closed
wants to merge 1 commit into from

Conversation

pitdicker
Copy link

This is based on #30.

Instead of panicking when reseeding failed, we now return an error via try_fill. For all other methods we ignore the failed reseed, and try again after the next amount of generation_threshold bytes is consumed.

If an error occurs, the cause chain is:
ErrorKind::Other > ReseedingError > error from Rsdr

I made some effort to return the error cause for std, and a static string "reseeding failed" in no_std mode.

Instead of panicking when reseeding failed, we now return an error via
`try_fill`. For all other methods we ignore the failed reseed, and try again
after the next amount of `generation_threshold` bytes is consumed.

If an error occurs, the cause chain is:
`ErrorKind::Other` > `ReseedingError` > error from `Rsdr`
@dhardy dhardy mentioned this pull request Nov 1, 2017
Copy link
Owner

@dhardy dhardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to miss the commits in dhardy:error_details.

self.bytes_generated += dest.len() as u64;
self.rng.fill_bytes(dest);
}

fn try_fill(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.reseed_if_necessary();
self.reseed_if_necessary()?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not keen on this difference of error handling between functions. Either the ReseedingRng should always allow continuation when reseeding fails (in which case this function should either silently continue or report ErrorKind::Transient and have some extra logic not to try reseeding too soon after a failure), or ReseedingRng should always fail if reseeding fails (with panic if necessary).

I'm not sure which; it could even be configurable but probably unnecessary. Perhaps allowing continuation after failure is the best option (users shouldn't rely on reseeding for security in any case).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we can return an error here and fill dest. Then it is up to the calling code to decide what to do.

/// An error that can occur when reseeding failed.
#[cfg(feature="std")]
#[derive(Debug)]
pub struct ReseedingError {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced we need another error type (see #10).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is to add a ReseedingFailed variant to the error kinds in rand_core.

It is just that I first explored this route (should have made a better PR message that said so).

@@ -109,9 +155,9 @@ impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R>> SeedableRng<(Rsdr, S)> for
}

/// Something that can be used to reseed an RNG via `ReseedingRng`.
pub trait Reseeder<R: ?Sized>: Debug {
pub trait Reseeder<R: ?Sized> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why lose Debug?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this API guideline was appropriate. Besides I see no functional need for the Debug bound here.

@dhardy
Copy link
Owner

dhardy commented Nov 2, 2017

A variation of this is now included in #30.

@pitdicker
Copy link
Author

Feel free to close this once anything that could be useful here is part of #30.

@dhardy dhardy closed this Nov 8, 2017
@pitdicker pitdicker deleted the Reseeding_errors branch November 11, 2017 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants