Fix panic in Arbitrary for Duration on nanosecond carry overflow - #657
Open
matthiasgoergens wants to merge 1 commit into
Open
Fix panic in Arbitrary for Duration on nanosecond carry overflow#657matthiasgoergens wants to merge 1 commit into
matthiasgoergens wants to merge 1 commit into
Conversation
Duration::new panics when carrying whole seconds out of the nanosecond argument overflows the seconds counter, so any::<Duration>() could in principle generate (u64::MAX, >= 1_000_000_000) and abort the test run. Under uniform generation this corner has probability ~2^-64 per case, which is presumably why it was never seen; it surfaced immediately while experimenting with a port of Hypothesis's edge-case-biased generation (which deliberately produces boundary values like u64::MAX). Keep the nanoseconds free of whole seconds when the seconds value is within carry range of u64::MAX; all other draws are unchanged. The regression test drives the actual strategy with a PassThrough RNG fed all-ones bytes, which deterministically forces the worst case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
any::<Duration>()can panic:Duration::newaborts when carrying whole seconds out of the nanosecond argument overflows the seconds counter, and the strategy mapsany::<(u64, u32)>()straight intoDuration::new(a, b). With uniform generation the failing corner (awithin carry range ofu64::MAXandb >= 1_000_000_000) has probability around 2^-64 per case, which is presumably why it has never been reported.I ran into it while experimenting with a port of Hypothesis's Conjecture engine (choice-tape generation and shrinking) to proptest: its edge-case-biased generator deliberately produces boundary values like
u64::MAXseconds, and this panic fell out of the very first full test run. I plan to propose that work separately; this fix stands on its own.The fix keeps the nanosecond argument free of whole seconds when the seconds value is within carry range of
u64::MAX; all other draws are unchanged. The regression test drives the actual strategy with aPassThroughRNG fed all-ones bytes, which deterministically forces(u64::MAX, u32::MAX)through generation; it panics without the fix.