Repository navigation
Avoid panics for full-width monotonic increments - #140
Open
perfloop[bot] wants to merge 3 commits into
Open
perfloop[bot] wants to merge 3 commits into
perfloop[bot] wants to merge 3 commits into
Conversation
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Author
|
PR head: I kept the existing Add path rather than applying the shorter update: it updates the high word only on carry and restores the low word when 80-bit overflow is detected. |
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
peterbourgon
approved these changes
Oct 27, 2026
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.
Monotonicaccepts auint64increment bound. With amath/randsource andinc > MaxInt64, a later publicNewcall at the same timestamp can panic if prior entropy is nonzero: the signed fast path converts the bound beforeInt63n. When prior entropy is zero,MonotonicReadreads fresh entropy instead. The constructor now keepsInt63nfor bounds throughMaxInt64and uses the existing byte sampler for larger bounds.inc=0still selectsMaxUint32; no public limit or API is added.uint80.Addnow usesbits.Add64. On carry, it checksu.Hiagainstmath.MaxUint16; it increments the high word only when the carry fits and returns before writing either word if the full 80-bit value would overflow. An overflowing addition returnsErrMonotonicOverflowand leaves the stored entropy at its last valid value instead of wrapping lower; a later increment can succeed if it fits. TheUnreleasedchangelog records the fix.TestMonotonicFullWidthIncrementcalls publicNewand usesmath/bigto verify unsigned bounds atMaxInt64,MaxInt64+1, andMaxUint64, a low-word carry, repeated overflow, and a high-bound reader error. It reproduces the two panic bounds and a lower-entropy retry failure on base. The intermediate that changed only the signed-bound path removes the panics but still fails the retry; the previous PR revision and this revision pass.TestMonotonicexercises theinc=0andinc=1controls, andTestMonotonicOverflowguards the defaultinc=0overflow error. Both pass on base, the signed-bound-only intermediate, the previous PR revision, and this revision.The Linux race-enabled suite,
go vet, gofmt, and Staticcheck passed. The macOS and Windows race-test jobs were not run because this environment is Linux-only; they remain target-CI work.This condition holds with this change and does not hold at
75921a7395850ea47ed4dfd832184a5a294c8fde:Public New with a nonzero initialized Monotonic entropy state and the same timestamp returns an ID whose unsigned 80-bit entropy increment is between 1 and the effective bound for math/rand-backed inc values 0, 1, MaxInt64, MaxInt64+1, and MaxUint64; with inc=1 at a non-overflowing low-word carry the increment is 1; a positive increment from MaxUint80 returns ErrMonotonicOverflow, and the next same-timestamp attempt also returns ErrMonotonicOverflow instead of succeeding with lower entropy; a high-bound entropy-read error is returned by New..The comparison harness used for the correctness assertion is not included in the pull request. Production frequency for high custom bounds is unknown; this repairs a supported-input boundary, not a measured incident.
@peterbourgon, please review.
Generated by Perfloop.