Repository navigation
[Java] Check var-data string length against the buffer before allocating - #1150
Open
slachiewicz wants to merge 2 commits into
Open
slachiewicz wants to merge 2 commits into
slachiewicz wants to merge 2 commits into
Conversation
slachiewicz
force-pushed
the
java-vardata-string-length-check
branch
from
November 13, 2026 22:26
8850c0b to
c642be4
Compare
mikeb01
reviewed
Nov 18, 2026
| final int headerLength = 2; | ||
| final int limit = parentMessage.limit(); | ||
| final int dataLength = (buffer.getShort(limit, BYTE_ORDER) & 0xFFFF); | ||
| if (dataLength > buffer.capacity() - limit - headerLength) |
Contributor
There was a problem hiding this comment.
Brackets would be preferred to make operator precedence explicit, e.g:
if (dataLength > (buffer.capacity() - limit) - headerLength)
slachiewicz
added a commit
to slachiewicz/simple-binary-encoding
that referenced
this pull request
Nov 19, 2026
Generated DTOs sized byte[] var data from a bounds-checked wrap. That relies on Agrona bounds checks being enabled; with agrona.disable.bounds.checks=true the wrap does not validate the length and an oversized/negative prefix leads to OOME or a native crash. Generate an explicit length check against the buffer capacity, current limit and header length (as in PR aeron-io#1150) before allocating, including a negative check for uint32 length fields.
slachiewicz
force-pushed
the
java-vardata-string-length-check
branch
from
November 19, 2026 21:00
c642be4 to
b7a741f
Compare
slachiewicz
added a commit
to slachiewicz/simple-binary-encoding
that referenced
this pull request
Nov 19, 2026
Generated DTOs sized byte[] var data from a bounds-checked wrap. That relies on Agrona bounds checks being enabled; with agrona.disable.bounds.checks=true the wrap does not validate the length and an oversized/negative prefix leads to OOME or a native crash. Generate an explicit length check against the buffer capacity, current limit and header length (as in PR aeron-io#1150) before allocating, including a negative check for uint32 length fields.
…ing. The generated String accessor must reject a length that is negative or runs past the buffer before it allocates the temporary byte[] or moves the limit; checking after the allocation lets the wire size it.
slachiewicz
force-pushed
the
java-vardata-string-length-check
branch
from
November 27, 2026 15:17
b7a741f to
f7e0b92
Compare
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.
The generated String accessor for var data allocated a byte[] of the wire length and advanced the limit before anything checked that length against the buffer, so a frame whose length prefix is negative or far larger than the message made the decoder allocate up to 2 GB, or fail with OutOfMemoryError, before it read past the buffer. The accessor now rejects such a length with IndexOutOfBoundsException before allocating or moving the limit. The IR codecs are regenerated with the same check.
The other var-data accessors (skip, wrap, Appendable) do not allocate and still rely on the buffer's own bounds checks.
Part of #969.
Verified: VarDataStringLengthTest fails on master (OutOfMemoryError: Requested array size exceeds VM limit) and passes here; sbe-tool checkstyle, tests and verifyJavaIrCodecsInSync pass.