[Java] implement a better fix for an out of bounds exception #865
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
ByteBuffer
like soByteBuffer.wrap(ptrBytes, 0, len)
, we are looking at a different portion of theptrBytes
array 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
ByteBuffer
in this case is[48, 49, 50, 51, 52, 53, 54, 55, 56]
because we start it at index 0. When checking ifptr + pos + 8 <= len
we get2 + 0 + 8 <= 9
which 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:
ByteBuffer
asByteBuffer.wrap(ptrBytes, ptr, len)
and remove theptr
offset in the bounds checks.ByteBuffer
asByteBuffer.wrap(ptrBytes, 0, ptrBytes.length)
and leave the bounds checks offset byptr
.CC: @headius