From 26e15586375eba0d9ab8e41270e6a53098dae9bd Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Sun, 25 Oct 2026 00:55:11 +0200 Subject: [PATCH 1/2] [Java] Size DTO var-data byte arrays from a bounds-checked wrap. The DTO decoder must take the array size from a bounds-checked wrap of the field, not from the length prefix, so a length past the end of the buffer fails before anything is allocated. --- .../sbe/generation/java/JavaDtoGenerator.java | 15 ++-- .../generation/java/DtoVarDataLengthTest.java | 84 +++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/DtoVarDataLengthTest.java diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java index 3cd0d9b4d2..51eab14d3f 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2025 Real Logic Limited. + * Copyright 2013-2026 Real Logic Limited. * Copyright (C) 2017 MarketFactory, Inc * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -811,11 +811,14 @@ private void generateVarDataDecodeWith( if (characterEncoding == null) { - decoderValueExtraction.append(blockIndent).append("byte[] ").append(dataVar) - .append(" = new byte[decoder.").append(formattedPropertyName).append("Length()];\n") - .append(blockIndent).append("decoder.get").append(toUpperFirstChar(formattedPropertyName)) - .append("(").append(dataVar).append(", 0, decoder.").append(formattedPropertyName) - .append("Length());\n"); + decoderValueExtraction.append(blockIndent).append("UnsafeBuffer ") + .append(dataVar).append("View = new UnsafeBuffer();\n") + .append(blockIndent).append("decoder.wrap").append(toUpperFirstChar(formattedPropertyName)) + .append("(").append(dataVar).append("View);\n") + .append(blockIndent).append("byte[] ").append(dataVar).append(" = new byte[") + .append(dataVar).append("View.capacity()];\n") + .append(blockIndent).append(dataVar).append("View.getBytes(0, ").append(dataVar) + .append(");\n"); } else { diff --git a/sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/DtoVarDataLengthTest.java b/sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/DtoVarDataLengthTest.java new file mode 100644 index 0000000000..7603993ee2 --- /dev/null +++ b/sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/DtoVarDataLengthTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2026 Real Logic Limited. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package uk.co.real_logic.sbe.generation.java; + +import dto_test.ExtendedCarDecoder; +import dto_test.ExtendedCarDto; +import dto_test.ExtendedCarEncoder; +import org.agrona.concurrent.UnsafeBuffer; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.nio.ByteOrder; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class DtoVarDataLengthTest +{ + private static final int MAX_FITTING_LENGTH = 8; + // added5 (uint32 length) and varCharData (uint16 length) follow activationCode, both left empty. + private static final int TRAILING_HEADERS_LENGTH = 4 + 2; + + // ExtendedCar with empty groups and strings, whose activationCode length prefix is followed by + // MAX_FITTING_LENGTH bytes and the empty trailing var data. + private static UnsafeBuffer carWithActivationCodeLength(final int length) + { + final UnsafeBuffer buffer = new UnsafeBuffer(new byte[256]); + final ExtendedCarEncoder encoder = new ExtendedCarEncoder().wrap(buffer, 0); + encoder.fuelFiguresCount(0); + encoder.performanceFiguresCount(0); + encoder.manufacturer(""); + encoder.model(""); + final int activationCodeOffset = encoder.limit(); + + final UnsafeBuffer message = new UnsafeBuffer( + new byte[activationCodeOffset + 4 + MAX_FITTING_LENGTH + TRAILING_HEADERS_LENGTH]); + message.putBytes(0, buffer, 0, activationCodeOffset); + message.putInt(activationCodeOffset, length, ByteOrder.LITTLE_ENDIAN); + return message; + } + + @ParameterizedTest + @ValueSource(ints = { Integer.MAX_VALUE, -1, MAX_FITTING_LENGTH + TRAILING_HEADERS_LENGTH + 1 }) + void shouldRejectByteArrayLengthBeyondBufferBeforeAllocating(final int length) + { + final UnsafeBuffer buffer = carWithActivationCodeLength(length); + + assertThrows( + IndexOutOfBoundsException.class, + () -> ExtendedCarDto.decodeFrom( + buffer, 0, ExtendedCarDecoder.BLOCK_LENGTH, ExtendedCarDecoder.SCHEMA_VERSION)); + } + + @ParameterizedTest + @ValueSource(ints = { 0, 3, MAX_FITTING_LENGTH }) + void shouldDecodeByteArrayThatFitsInBuffer(final int length) + { + final UnsafeBuffer buffer = carWithActivationCodeLength(length); + final byte[] expected = new byte[length]; + for (int i = 0; i < length; i++) + { + expected[i] = (byte)(i + 1); + } + buffer.putBytes(buffer.capacity() - TRAILING_HEADERS_LENGTH - MAX_FITTING_LENGTH, expected); + + final ExtendedCarDto dto = ExtendedCarDto.decodeFrom( + buffer, 0, ExtendedCarDecoder.BLOCK_LENGTH, ExtendedCarDecoder.SCHEMA_VERSION); + + assertArrayEquals(expected, dto.activationCode()); + } +} From c90f2dfe92243a3e52a9081e41478b5fc0dc7ec8 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Thu, 19 Nov 2026 18:36:45 +0100 Subject: [PATCH 2/2] [Java] Check DTO var-data length explicitly before allocating 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 #1150) before allocating, including a negative check for uint32 length fields. --- .../sbe/generation/java/JavaDtoGenerator.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java index 51eab14d3f..959a2f693f 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java @@ -811,14 +811,24 @@ private void generateVarDataDecodeWith( if (characterEncoding == null) { - decoderValueExtraction.append(blockIndent).append("UnsafeBuffer ") - .append(dataVar).append("View = new UnsafeBuffer();\n") - .append(blockIndent).append("decoder.wrap").append(toUpperFirstChar(formattedPropertyName)) - .append("(").append(dataVar).append("View);\n") + final Token lengthToken = Generators.findFirst("length", tokens, i); + final String negativeLengthCheck = PrimitiveType.UINT32 == lengthToken.encoding().primitiveType() ? + dataVar + "Length < 0 || " : ""; + + decoderValueExtraction.append(blockIndent).append("final int ").append(dataVar) + .append("Length = decoder.").append(formattedPropertyName).append("Length();\n") + .append(blockIndent).append("if (").append(negativeLengthCheck).append(dataVar) + .append("Length > decoder.buffer().capacity() - decoder.limit() - ").append(decoderClassName) + .append(".").append(formattedPropertyName).append("HeaderLength())\n") + .append(blockIndent).append("{\n") + .append(blockIndent).append(INDENT) + .append("throw new IndexOutOfBoundsException(\"var data length=\" + ") + .append(dataVar).append("Length);\n") + .append(blockIndent).append("}\n") .append(blockIndent).append("byte[] ").append(dataVar).append(" = new byte[") - .append(dataVar).append("View.capacity()];\n") - .append(blockIndent).append(dataVar).append("View.getBytes(0, ").append(dataVar) - .append(");\n"); + .append(dataVar).append("Length];\n") + .append(blockIndent).append("decoder.get").append(toUpperFirstChar(formattedPropertyName)) + .append("(").append(dataVar).append(", 0, ").append(dataVar).append("Length);\n"); } else {