[release/v1] Fix integer underflow in BITCOUNT ... BIT byte-boundary mask (backport #1994) - #1998
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR backports the fix for a BITCOUNT ... BIT edge case where ranges ending exactly on a byte boundary could produce a wrapping mask and incorrectly count unset bits as set.
Changes:
- Fix
BitmapManagerBitCount.BitIndexCount(byte* ...)to compute the right (exclusive) bit index as(endOffset & 7) + 1, preventing byte-boundary wrap to0. - Add a regression test for the byte-boundary underflow scenario (including single-bit and boundary-crossing ranges).
- Update an existing long-negative-offset parsing test expectation to reflect correct modulo-wrapping semantics after the fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/Garnet.test/GarnetBitmapTests.cs | Adds a regression test for the byte-boundary BIT underflow and updates an existing assertion impacted by the corrected mask logic. |
| libs/server/Resp/Bitmap/BitmapManagerBitCount.cs | Fixes right-bit-index calculation to avoid byte-boundary wraparound and mask underflow in BIT-mode counting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
badrishc
approved these changes
Jul 30, 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.
Backport of #1994 to
release/v1.Summary
Fixes an integer underflow in
BITCOUNT ... BIT(BitmapManagerBitCount.cs) that returned non-zero counts for unset bits whenever a BIT-mode range ended on a byte boundary (endOffset % 8 == 7).The inclusive right bit index was computed as
(int)((endOffset + 1) & 7), which wraps to0at byte boundaries and produces a wrapping byte-mask underflow. Fixed to use the same formula already used correctly inBitmapManagerBitPos.cs:Tests
BitmapBitCountBitBoundaryUnderflowTest(byte0x80) covering single-bit and boundary-crossing BIT ranges.BitmapBitCountLongBitOffsetParsingTest: its previous expectation of8was an artifact of the underflow. Under Garnet''s modulo-wrapping negative-offset semantics the clamped start wraps to bit index 1, so bits 1..7 of0xFF=7.release/v1(net10.0). TheNormalizeBitCountOffsets/MaxOffsetForBitmapLength/ProcessNegativeOffsetsemantics are identical on this branch.Cherry-picked from squash-merge 52aef6e (#1994 on
main).