Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ public void addAllNeededImports(
@Override
public String parseCode() {
if (type == FieldType.MESSAGE) {
return "%s.PROTOBUF.parse(input, strictMode, parseUnknownFields, maxDepth - 1)".formatted(messageType);
return "%s.PROTOBUF.parse(input, strictMode, parseUnknownFields, maxDepth - 1, maxSize)"
.formatted(messageType);
} else {
return "input";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.hedera.pbj.runtime.Codec;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.pbj.runtime.io.buffer.BufferedData;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.hedera.pbj.test.proto.pbj.Everything;
import com.hedera.pbj.test.proto.pbj.InnerEverything;
import com.hedera.pbj.test.proto.pbj.MessageWithBytes;
import com.hedera.pbj.test.proto.pbj.MessageWithString;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -73,4 +76,45 @@ void testCustomMaxSize() throws Exception {
assertThrows(
ParseException.class, () -> MessageWithString.PROTOBUF.parse(data, false, false, Integer.MAX_VALUE, 6));
}

@Test
void testNestedMaxSize() throws Exception {
// This message, an inner nested message within it, as well as a field in that inner message all exceed the
// DEFAULT_MAX_SIZE:
final Everything everything = Everything.newBuilder()
.innerEverything(
InnerEverything.newBuilder().bytesField(Bytes.wrap("1".repeat(Codec.DEFAULT_MAX_SIZE + 1))))
.build();
final Bytes bytes = Everything.PROTOBUF.toBytes(everything);

// Try negative cases first:
assertThrows(ParseException.class, () -> Everything.PROTOBUF.parse(bytes.toReadableSequentialData()));
assertThrows(
ParseException.class,
() -> Everything.PROTOBUF.parse(
bytes.toReadableSequentialData(), false, false, Codec.DEFAULT_MAX_DEPTH, 256));
assertThrows(
ParseException.class,
() -> Everything.PROTOBUF.parse(
bytes.toReadableSequentialData(),
false,
false,
Codec.DEFAULT_MAX_DEPTH,
Codec.DEFAULT_MAX_SIZE));
// +1 still shouldn't work because the outer and the inner objects are still larger:
assertThrows(
ParseException.class,
() -> Everything.PROTOBUF.parse(
bytes.toReadableSequentialData(),
false,
false,
Codec.DEFAULT_MAX_DEPTH,
Codec.DEFAULT_MAX_SIZE + 1));

// Now try supplying a large enough maxSize to parse it:
final Everything parsedEverything = Everything.PROTOBUF.parse(
bytes.toReadableSequentialData(), false, false, Codec.DEFAULT_MAX_DEPTH, Codec.DEFAULT_MAX_SIZE * 2);

assertEquals(everything, parsedEverything);
}
}
Loading