Skip to content

Conversation

@whisk
Copy link
Contributor

@whisk whisk commented Nov 15, 2025

Problem

When doing the ShiftRight for n * wordSize bits (the happy case), idx may end to be equal to the length of set, leading to out of range runtime error when zeroing the pages, see example:

--- FAIL: TestShiftRightFull (0.00s)
panic: runtime error: index out of range [15] with length 15 [recovered, repanicked]

goroutine 187 [running]:
testing.tRunner.func1.2({0x10089a580, 0x14000266b10})
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1872 +0x190
testing.tRunner.func1()
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1875 +0x31c
panic({0x10089a580?, 0x14000266b10?})
	/opt/homebrew/opt/go/libexec/src/runtime/panic.go:783 +0x120
github.com/bits-and-blooms/bitset.(*BitSet).ShiftRight(0x140004125c0?, 0x3c1?)
	/Users/whisk/devel/github.com/whisk/bitset/bitset.go:1560 +0x234
github.com/bits-and-blooms/bitset.TestShiftRightFull.func1(...)
	/Users/whisk/devel/github.com/whisk/bitset/bitset_test.go:2461
github.com/bits-and-blooms/bitset.TestShiftRightFull(0x14000416c40)
	/Users/whisk/devel/github.com/whisk/bitset/bitset_test.go:2473 +0x124
testing.tRunner(0x14000416c40, 0x1008aecc0)
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1934 +0xc8
created by testing.(*T).Run in goroutine 1
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1997 +0x364
FAIL	github.com/bits-and-blooms/bitset	0.388s
FAIL

The added testcase fails without the fix.

Fix

Capped iteration to the length of set, no to idx.

Misc

I plan to add some fuzz tests later to help with spotting similar errors in future.

@whisk whisk force-pushed the fix/ShiftRight-pages-zeroing branch from d933b4e to f0f8af2 Compare November 15, 2025 20:57
@lemire lemire merged commit 8c1b8bc into bits-and-blooms:master Nov 16, 2025
29 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