[Java] implement a better fix for an out of bounds exception#865
Merged
byroot merged 1 commit intoruby:masterfrom Sep 20, 2025
Merged
[Java] implement a better fix for an out of bounds exception#865byroot merged 1 commit intoruby:masterfrom
byroot merged 1 commit intoruby:masterfrom
Conversation
headius
approved these changes
Sep 20, 2025
Contributor
headius
left a comment
There was a problem hiding this comment.
Either fix would be fine from a style perspective so it's just a matter of what fits the flow of the code best while un-breaking SWAR. I see you went with the first option and I think that's just fine.
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.
After thinking about #859 and the fix #860 all day, something didn't feel quite right. After doing some debugging tonight, I realized the fix might defeat some of the SWAR optimizations.
When creating the
ByteBufferlike soByteBuffer.wrap(ptrBytes, 0, len), we are looking at a different portion of theptrBytesarray whenptr > 0. While we end up reading the correct chunk withbb.getLong(ptr + pos)orbb.getInt(ptr + pos), we may end up terminating the SWAR-optimized loop early.Consider:
The backing array has a length of 15 bytes but the length of the string is 9 bytes.
The
ByteBufferin this case is[48, 49, 50, 51, 52, 53, 54, 55, 56]because we start it at index 0. When checking ifptr + pos + 8 <= lenwe get2 + 0 + 8 <= 9which isfalse.We really should be looking at
[50, 51, 52, 53, 54, 55, 56, 57, 48]assuming we do not offset the bounds check byptr, as we did prior to the fix earlier today.There are two possible fixes:
ByteBufferasByteBuffer.wrap(ptrBytes, ptr, len)and remove theptroffset in the bounds checks.ByteBufferasByteBuffer.wrap(ptrBytes, 0, ptrBytes.length)and leave the bounds checks offset byptr.CC: @headius