Skip to content

Fix flaky TestSetAndGetUUIDValue: big.Int drops leading zero bytes#518

Merged
lemire merged 1 commit intoRoaringBitmap:masterfrom
anacrolix:bsi-uuid-fix
Mar 9, 2026
Merged

Fix flaky TestSetAndGetUUIDValue: big.Int drops leading zero bytes#518
lemire merged 1 commit intoRoaringBitmap:masterfrom
anacrolix:bsi-uuid-fix

Conversation

@anacrolix
Copy link
Contributor

@anacrolix anacrolix commented Mar 9, 2026

Summary

TestSetAndGetUUIDValue fails intermittently (~1/256 runs) when the UUID
binary representation starts with a 0x00 byte. big.Int.Bytes() returns
the minimal-length byte slice with no leading zeros, so that byte is
silently dropped, producing 15 bytes instead of 16. uuid.FromBytes then
rejects the result with "invalid UUID (got 15 bytes)".

Fix: use big.Int.FillBytes instead, which left-pads with zeros to
exactly fill the destination slice.

Also adds TestSetAndGetUUIDValueLeadingZero, a deterministic regression
test that constructs a UUID with a known leading zero byte so the failure
mode is reproducible without relying on randomness.

Verified by running TestSetAndGetUUIDValue with -count=500 before and
after the fix: the unfixed test reliably produces a failure within that
window; the fixed test passes all 500 runs.

This was flagged by the macOS CI failure on #515, which is otherwise an
unrelated change. The Linux runners happened to not hit the 1/256 case in
that run.

…eros

big.Int.Bytes() returns the minimal-length byte slice with no leading
zeros. When the UUID binary representation happens to start with 0x00
(probability ~1/256), the round-trip through big.Int loses that byte,
producing 15 bytes instead of 16. uuid.FromBytes then rejects the result.

Fix by using big.Int.FillBytes, which left-pads with zeros to exactly
fill the destination slice.

Also add a deterministic regression test, TestSetAndGetUUIDValueLeadingZero,
which constructs a UUID with a known leading zero byte so the failure mode
can be reproduced without relying on randomness.
@anacrolix anacrolix marked this pull request as ready for review March 9, 2026 11:16
assert.Equal(t, bigUUID, bv)

newUUID, err := uuid.FromBytes(bv.Bytes())
// big.Int.Bytes() drops leading zero bytes; use FillBytes to restore the
Copy link
Member

Choose a reason for hiding this comment

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

@lemire lemire merged commit b4a7332 into RoaringBitmap:master Mar 9, 2026
8 checks passed
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