Skip to content

Commit 75500bd

Browse files
fix: forward maxSize when parsing nested messages (#700)
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
1 parent e4b7805 commit 75500bd

File tree

2 files changed

+46
-1
lines changed
  • pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl
  • pbj-integration-tests/src/test/java/com/hedera/pbj/integration/test

2 files changed

+46
-1
lines changed

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/SingleField.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ public void addAllNeededImports(
216216
@Override
217217
public String parseCode() {
218218
if (type == FieldType.MESSAGE) {
219-
return "%s.PROTOBUF.parse(input, strictMode, parseUnknownFields, maxDepth - 1)".formatted(messageType);
219+
return "%s.PROTOBUF.parse(input, strictMode, parseUnknownFields, maxDepth - 1, maxSize)"
220+
.formatted(messageType);
220221
} else {
221222
return "input";
222223
}

pbj-integration-tests/src/test/java/com/hedera/pbj/integration/test/MaxSizeTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertThrows;
66

7+
import com.hedera.pbj.runtime.Codec;
78
import com.hedera.pbj.runtime.ParseException;
89
import com.hedera.pbj.runtime.io.buffer.BufferedData;
910
import com.hedera.pbj.runtime.io.buffer.Bytes;
11+
import com.hedera.pbj.test.proto.pbj.Everything;
12+
import com.hedera.pbj.test.proto.pbj.InnerEverything;
1013
import com.hedera.pbj.test.proto.pbj.MessageWithBytes;
1114
import com.hedera.pbj.test.proto.pbj.MessageWithString;
1215
import org.junit.jupiter.api.Test;
@@ -73,4 +76,45 @@ void testCustomMaxSize() throws Exception {
7376
assertThrows(
7477
ParseException.class, () -> MessageWithString.PROTOBUF.parse(data, false, false, Integer.MAX_VALUE, 6));
7578
}
79+
80+
@Test
81+
void testNestedMaxSize() throws Exception {
82+
// This message, an inner nested message within it, as well as a field in that inner message all exceed the
83+
// DEFAULT_MAX_SIZE:
84+
final Everything everything = Everything.newBuilder()
85+
.innerEverything(
86+
InnerEverything.newBuilder().bytesField(Bytes.wrap("1".repeat(Codec.DEFAULT_MAX_SIZE + 1))))
87+
.build();
88+
final Bytes bytes = Everything.PROTOBUF.toBytes(everything);
89+
90+
// Try negative cases first:
91+
assertThrows(ParseException.class, () -> Everything.PROTOBUF.parse(bytes.toReadableSequentialData()));
92+
assertThrows(
93+
ParseException.class,
94+
() -> Everything.PROTOBUF.parse(
95+
bytes.toReadableSequentialData(), false, false, Codec.DEFAULT_MAX_DEPTH, 256));
96+
assertThrows(
97+
ParseException.class,
98+
() -> Everything.PROTOBUF.parse(
99+
bytes.toReadableSequentialData(),
100+
false,
101+
false,
102+
Codec.DEFAULT_MAX_DEPTH,
103+
Codec.DEFAULT_MAX_SIZE));
104+
// +1 still shouldn't work because the outer and the inner objects are still larger:
105+
assertThrows(
106+
ParseException.class,
107+
() -> Everything.PROTOBUF.parse(
108+
bytes.toReadableSequentialData(),
109+
false,
110+
false,
111+
Codec.DEFAULT_MAX_DEPTH,
112+
Codec.DEFAULT_MAX_SIZE + 1));
113+
114+
// Now try supplying a large enough maxSize to parse it:
115+
final Everything parsedEverything = Everything.PROTOBUF.parse(
116+
bytes.toReadableSequentialData(), false, false, Codec.DEFAULT_MAX_DEPTH, Codec.DEFAULT_MAX_SIZE * 2);
117+
118+
assertEquals(everything, parsedEverything);
119+
}
76120
}

0 commit comments

Comments
 (0)