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

Fix BitArray#toggle when toggling empty subrange #11381

Conversation

HertzDevil
Copy link
Contributor

When #toggle is called on a BitArray with an empty subrange and a start index that is a multiple of 32, the method incorrectly toggles the entire UInt32s at and before the given start index:

x = BitArray.new(70)
x.to_slice # => Bytes[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
x.toggle(64, 0)
x.to_slice # => Bytes[0, 0, 0, 0, 255, 255, 255, 255, 255]
# note that `x` also has its unused bits set

This PR fixes it by adding an early return for count == 0.

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection labels Oct 28, 2021
@straight-shoota straight-shoota added this to the 1.3.0 milestone Oct 28, 2021
@beta-ziliani beta-ziliani modified the milestones: 1.3.0, 1.2.2 Oct 29, 2021
@straight-shoota straight-shoota merged commit 347ba2d into crystal-lang:master Nov 1, 2021
straight-shoota pushed a commit to straight-shoota/crystal that referenced this pull request Nov 1, 2021
@HertzDevil HertzDevil deleted the bug/bitarray-toggle-count-zero branch November 2, 2021 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants