Skip to content

Fix readInlineArray reading from the wrong index when readerIndex is non-zero - #3605

Merged
Lukasa merged 1 commit into
apple:mainfrom
adityasingh2400:fix-read-inline-array-reader-index
May 26, 2026
Merged

Lukasa merged 1 commit into
apple:mainfrom
adityasingh2400:fix-read-inline-array-reader-index

Conversation

@adityasingh2400

Copy link
Copy Markdown
Contributor

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.

…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.
@adityasingh2400

Copy link
Copy Markdown
Contributor Author

@Lukasa this reopens the change from #3604, which I closed inadvertently. Thank you for offering to merge it. I had deleted the original fork and branch, so reopening #3604 was not possible, and this is a fresh PR with the identical commit (752ceb5). Happy to adjust anything if needed.

@Lukasa Lukasa added the πŸ”¨ semver/patch No public API change. label May 26, 2026
@Lukasa

Lukasa commented May 26, 2026

Copy link
Copy Markdown
Contributor

Thanks for filing this!

@Lukasa Lukasa closed this May 26, 2026
@Lukasa Lukasa reopened this May 26, 2026
@Lukasa
Lukasa enabled auto-merge (squash) May 26, 2026 08:54
@Lukasa
Lukasa merged commit d20fa05 into apple:main May 26, 2026
141 of 163 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

πŸ”¨ semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants