Skip to content

[Java] Check var-data string length against the buffer before allocating - #1150

Open
slachiewicz wants to merge 2 commits into
aeron-io:masterfrom
slachiewicz:java-vardata-string-length-check
Open

slachiewicz wants to merge 2 commits into
aeron-io:masterfrom
slachiewicz:java-vardata-string-length-check

Conversation

@slachiewicz

Copy link
Copy Markdown
Contributor

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.

final int headerLength = 2;
final int limit = parentMessage.limit();
final int dataLength = (buffer.getShort(limit, BYTE_ORDER) & 0xFFFF);
if (dataLength > buffer.capacity() - limit - headerLength)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
slachiewicz force-pushed the java-vardata-string-length-check branch from c642be4 to b7a741f Compare November 19, 2026 21:00
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
slachiewicz force-pushed the java-vardata-string-length-check branch from b7a741f to f7e0b92 Compare November 27, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants