Files
swift-nio/Sources
Aditya Singh d20fa05a64 Fix readInlineArray reading from the wrong index when readerIndex is non-zero (#3605)
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.
2026-05-26 09:03:23 +00:00
..
2024-09-12 13:18:25 +01:00
2025-10-06 10:58:33 +01:00
2025-05-07 12:24:44 +00:00
2025-05-07 12:24:44 +00:00