Skip to content

Commit 0681ada

Browse files
committed
Merge branch '2.15'
2 parents a52427d + 82a0db9 commit 0681ada

File tree

6 files changed

+118
-11
lines changed

6 files changed

+118
-11
lines changed

avro/src/test/java/tools/jackson/dataformat/avro/RoundtripTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tools.jackson.dataformat.avro;
22

3+
import tools.jackson.core.StreamReadConstraints;
34
import tools.jackson.core.StreamWriteFeature;
45

56
import tools.jackson.databind.*;
@@ -73,13 +74,8 @@ private void _testCharSequences(ObjectMapper mapper) throws Exception
7374
input.id = "123";
7475
input.name = "John";
7576

76-
byte[] avroData;
77-
try {
78-
avroData = writ.writeValueAsBytes(input);
79-
assertNotNull(avroData);
80-
} catch (Exception e) {
81-
throw e;
82-
}
77+
byte[] avroData = writ.writeValueAsBytes(input);
78+
assertNotNull(avroData);
8379

8480
CharSeqBean output = mapper.reader(CHARSEQ_SCHEMA)
8581
.forType(CharSeqBean.class).readValue(avroData);
@@ -88,4 +84,30 @@ private void _testCharSequences(ObjectMapper mapper) throws Exception
8884
assertEquals(input.id, output.id);
8985
assertEquals(input.name, output.name);
9086
}
87+
88+
public void testCharSequencesLowStringLimit() throws Exception
89+
{
90+
AvroFactory factory = AvroFactory.builder()
91+
.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(1).build())
92+
.build();
93+
ObjectMapper mapper = AvroMapper.builder(factory)
94+
.enable(StreamWriteFeature.IGNORE_UNKNOWN)
95+
.build();
96+
ObjectWriter writ = mapper.writer(CHARSEQ_SCHEMA);
97+
98+
CharSeqBean input = new CharSeqBean();
99+
input.id = "123";
100+
input.name = "John";
101+
102+
byte[] avroData = writ.writeValueAsBytes(input);
103+
assertNotNull(avroData);
104+
105+
try {
106+
mapper.reader(CHARSEQ_SCHEMA)
107+
.forType(CharSeqBean.class).readValue(avroData);
108+
fail("expected IllegalStateException");
109+
} catch (IllegalStateException ise) {
110+
assertEquals("String length (3) exceeds the maximum length (1)", ise.getMessage());
111+
}
112+
}
91113
}

cbor/src/test/java/tools/jackson/dataformat/cbor/seq/ReadTreesTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.List;
44

5+
import tools.jackson.core.StreamReadConstraints;
56
import tools.jackson.databind.*;
7+
import tools.jackson.dataformat.cbor.CBORFactory;
68
import tools.jackson.dataformat.cbor.CBORTestBase;
79

810
public class ReadTreesTest extends CBORTestBase
@@ -61,4 +63,27 @@ public void testReadTreeSequence() throws Exception
6163
/* not differ)
6264
/**********************************************************
6365
*/
66+
67+
public void testReadTreeSequenceLowStringLimit() throws Exception
68+
{
69+
final byte[] INPUT = concat(
70+
cborDoc(a2q("{\"id\":1, \"value\":137 }")),
71+
cborDoc(a2q("{\"id\":2, \"value\":256 }\n")),
72+
cborDoc(a2q("{\"id\":3, \"value\":-89 }"))
73+
);
74+
75+
CBORFactory cborFactory = cborFactoryBuilder()
76+
.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(1).build())
77+
.build();
78+
try (MappingIterator<JsonNode> it = cborMapper(cborFactory).readerFor(JsonNode.class)
79+
.readValues(INPUT)) {
80+
assertTrue(it.hasNextValue());
81+
try {
82+
it.nextValue();
83+
fail("expected IllegalStateException");
84+
} catch (IllegalStateException ise) {
85+
assertEquals("String length (2) exceeds the maximum length (1)", ise.getMessage());
86+
}
87+
}
88+
}
6489
}

protobuf/src/main/java/tools/jackson/dataformat/protobuf/ProtobufParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public ProtobufParser(ObjectReadContext readCtxt, IOContext ioCtxt,
301301
_inputPtr = start;
302302
_inputEnd = end;
303303
_bufferRecyclable = bufferRecyclable;
304-
_textBuffer = ioCtxt.constructTextBuffer();
304+
_textBuffer = ioCtxt.constructReadConstrainedTextBuffer();
305305
_streamReadContext = ProtobufReadContext.createRootContext();
306306

307307
_tokenInputRow = -1;

protobuf/src/test/java/tools/jackson/dataformat/protobuf/ReadSimpleTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
import tools.jackson.core.JsonParser;
77
import tools.jackson.core.JsonToken;
8+
import tools.jackson.core.StreamReadConstraints;
89
import tools.jackson.core.StreamReadFeature;
9-
10+
import tools.jackson.databind.DatabindException;
1011
import tools.jackson.databind.ObjectMapper;
1112
import tools.jackson.databind.ObjectWriter;
1213

@@ -366,4 +367,28 @@ public void testSkipUnknown() throws Exception
366367
assertEquals(input.x, result.x);
367368
assertEquals(input.y, result.y);
368369
}
370+
371+
public void testStringArraySimpleLowLimit() throws Exception
372+
{
373+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_STRINGS);
374+
ProtobufFactory protobufFactory = ProtobufFactory.builder()
375+
.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(1).build())
376+
.build();
377+
ProtobufMapper protobufMapper = ProtobufMapper.builder(protobufFactory).build();
378+
final ObjectWriter w = protobufMapper.writerFor(Strings.class)
379+
.with(schema);
380+
Strings input = new Strings("Dogs", "like", "Baco\u00F1");
381+
byte[] bytes = w.writeValueAsBytes(input);
382+
assertNotNull(bytes);
383+
assertEquals(20, bytes.length);
384+
385+
try {
386+
protobufMapper.readerFor(Strings.class).with(schema).readValue(bytes);
387+
fail("Expected DatabindException");
388+
} catch (DatabindException jme) {
389+
String message = jme.getMessage();
390+
assertTrue("unexpected message: " + message,
391+
message.startsWith("String length (4) exceeds the maximum length (1)"));
392+
}
393+
}
369394
}

smile/src/main/java/tools/jackson/dataformat/smile/SmileParserBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public SmileParserBase(ObjectReadContext readCtxt, IOContext ioCtxt,
249249
? DupDetector.rootDetector(this) : null;
250250
_streamReadContext = SimpleStreamReadContext.createRootContext(dups);
251251

252-
_textBuffer = ioCtxt.constructTextBuffer();
252+
_textBuffer = ioCtxt.constructReadConstrainedTextBuffer();
253253
_smileBufferRecycler = _smileBufferRecycler();
254254
}
255255

smile/src/test/java/tools/jackson/dataformat/smile/async/SimpleStringArrayTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
import tools.jackson.core.JsonGenerator;
77
import tools.jackson.core.JsonToken;
8-
8+
import tools.jackson.core.StreamReadConstraints;
99
import tools.jackson.databind.ObjectWriter;
10+
11+
import tools.jackson.dataformat.smile.SmileFactory;
1012
import tools.jackson.dataformat.smile.SmileGenerator;
13+
import tools.jackson.dataformat.smile.SmileParser;
14+
import tools.jackson.dataformat.smile.databind.SmileMapper;
1115

1216
public class SimpleStringArrayTest extends AsyncTestBase
1317
{
@@ -101,6 +105,37 @@ public void testLongAsciiStrings() throws IOException
101105
_testStrings(input, data, 1, 1);
102106
}
103107

108+
public void testLongAsciiStringsLowStringLimit() throws IOException
109+
{
110+
final String[] input = new String[] {
111+
// ~100 chars for long(er) content
112+
String.format("%s %s %s %s %s %s %s %s %s %s %s %s",
113+
str0to9,str0to9,"...",str0to9,"/", str0to9,
114+
str0to9,"",str0to9,str0to9,"...",str0to9),
115+
LONG_ASCII
116+
};
117+
SmileFactory f = SmileFactory.builder()
118+
.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(10).build())
119+
.enable(SmileParser.Feature.REQUIRE_HEADER)
120+
.enable(SmileGenerator.Feature.CHECK_SHARED_NAMES)
121+
.enable(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES)
122+
.build();
123+
SmileMapper mapper = new SmileMapper(f);
124+
byte[] data = _stringDoc(mapper.writer(), input);
125+
126+
AsyncReaderWrapper r = asyncForBytes(mapper, 1, data, 0);
127+
// start with "no token"
128+
assertNull(r.currentToken());
129+
assertToken(JsonToken.START_ARRAY, r.nextToken());
130+
assertToken(JsonToken.VALUE_STRING, r.nextToken());
131+
try {
132+
r.currentText();
133+
fail("expected IllegalStateException");
134+
} catch (IllegalStateException ise) {
135+
assertEquals("String length (98) exceeds the maximum length (10)", ise.getMessage());
136+
}
137+
}
138+
104139
public void testLongUnicodeStrings() throws IOException
105140
{
106141
// ~100 chars for long(er) content

0 commit comments

Comments
 (0)