Fix int mask sign extension in BitField long methods#1711
Open
alhudz wants to merge 1 commit into
Open
Conversation
| public BitField(final int mask) { | ||
| this.mask = mask; | ||
| this.mask = Integer.toUnsignedLong(mask); | ||
| this.shiftCount = mask == 0 ? 0 : Integer.numberOfTrailingZeros(mask); |
Member
There was a problem hiding this comment.
Hello @alhudz
Thank you for the PR.
Should the argument to numberOfTrailingZeros be mask or this.mask?
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.
Repro:
new BitField(0x80000000), thengetRawValue(-1L)/clear(-1L).Expected:
getRawValue(-1L)==0x80000000andclear(-1L)==0xFFFFFFFF7FFFFFFF, since only bit 31 belongs to the field.Actual:
getRawValue(-1L)==0xFFFFFFFF80000000andclear(-1L)==0x7FFFFFFF, so bits 32-63 leak into the field.Cause: the
long maskfield and the long-typed methods arrived in 3.21.0, butBitField(int)still assigns the int mask straight into thelongfield, so any mask with bit 31 set is sign-extended across bits 32-63. The int-typed methods cast back tointand hide it, the long ones do not.Fix: build the field with
Integer.toUnsignedLong(mask)so the int mask only occupies the low 32 bits.