Repository navigation
Fix readInlineArray reading from the wrong index when readerIndex is non-zero - #3605
Merged
Lukasa merged 1 commit intoMay 26, 2026
Merged
Conversation
β¦non-zero Motivation: ByteBuffer.readInlineArray reads each element with getInteger(at:), whose index parameter is an absolute index into the buffer. The loop passed `stride * index`, which starts at zero and ignores the reader index. As long as the buffer's reader index is zero the absolute index happens to line up with the intended bytes, which is why the existing tests passed. On any buffer whose reader index has advanced (the common case once some bytes have already been read), getInteger(at:) is handed an index that is below the reader index, returns nil, and the force-unwrap traps. A public read API therefore crashes the process on perfectly valid input. Modifications: Offset the per-element index by the reader index so getInteger(at:) receives the correct absolute index. Added a regression test that reads a short prefix first so the reader index is non-zero, then reads an InlineArray and checks the values and indices. Result: readInlineArray works regardless of the reader index. The new test traps before this change and passes after it; the existing InlineArray tests still pass.
Contributor
Author
Lukasa
approved these changes
May 26, 2026
Contributor
|
Thanks for filing this! |
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.
ByteBuffer.readInlineArray reads each element through getInteger(at:). The index parameter of getInteger(at:) is an absolute index into the buffer, the same one readInteger passes self.readerIndex into. The loop in readInlineArray instead passed stride * index, which starts at zero and never accounts for the reader index.
As long as the buffer has a reader index of zero this absolute index lines up with the bytes the caller expects, which is why the current tests pass: they all read from a freshly written buffer. On any buffer whose reader index has already moved (the normal case once some bytes have been consumed, for example reading a length prefix before the array), getInteger(at:) is given an index below the reader index. rangeWithinReadableBytes rejects it, getInteger returns nil, and the force-unwrap on the result traps. So a public read API crashes the process on entirely valid input.
The fix snapshots the reader index before building the array and offsets each element index by it, so getInteger(at:) gets the correct absolute index. The multiplication stays bounded by bytesRequired as before, and adding the reader index cannot overflow because the result stays within the writer index.
I added a regression test that writes a short prefix, reads it off so the reader index is non-zero, then reads an InlineArray and checks both the element values and the resulting reader index. It traps before this change and passes after it. The three existing InlineArray tests, the full ByteBufferTest suite (275 tests), and ByteBufferSpanTests all still pass.
Reopening the change from #3604, which I closed inadvertently. The original branch and fork were deleted, so this is a fresh PR with the identical commit.