Skip to content

Commit 916377d

Browse files
committed
(backport from 2.21) Fix #649 (#650)
1 parent d2ab14b commit 916377d

5 files changed

Lines changed: 35 additions & 13 deletions

File tree

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ protected JsonParser _createParser(char[] data, int offset, int len, IOContext c
424424
@Override
425425
protected CBORParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
426426
{
427+
// [core#1548] Validate doc length up front for fixed buffers
428+
_streamReadConstraints.validateDocumentLength(len);
427429
return new CBORParserBootstrapper(ctxt, data, offset, len).constructParser(
428430
_factoryFeatures, _parserFeatures, _formatParserFeatures,
429431
_objectCodec, _byteSymbolCanonicalizer);

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/gen/constraints/LongDocumentCBORReadTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import com.fasterxml.jackson.core.StreamReadConstraints;
1010
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
1111

12-
import com.fasterxml.jackson.databind.ObjectMapper;
1312
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
1413
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
14+
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper;
1515

1616
public class LongDocumentCBORReadTest extends CBORTestBase
1717
{
18-
private final ObjectMapper MAPPER_VANILLA = cborMapper();
18+
private final CBORMapper MAPPER_VANILLA = cborMapper();
1919

20-
private final ObjectMapper MAPPER_CONSTRAINED = cborMapper(
20+
private final CBORMapper MAPPER_CONSTRAINED = cborMapper(
2121
CBORFactory.builder()
2222
// limit to 100kB doc reads
2323
.streamReadConstraints(StreamReadConstraints.builder()
@@ -29,13 +29,20 @@ public void testLongDocumentConstraint() throws Exception
2929
{
3030
// Need a bit longer than minimum since checking is approximate, not exact
3131
byte[] doc = createBigDoc(60_000);
32-
// Must read from `InputStream` as validation is during "loadMore()":
33-
try (JsonParser p = MAPPER_CONSTRAINED.createParser(new ByteArrayInputStream(doc))) {
32+
_testLongDocumentConstraint(doc, true);
33+
// [dataformats-binary#649] fixed buffer too
34+
_testLongDocumentConstraint(doc, false);
35+
}
36+
37+
private void _testLongDocumentConstraint(byte[] doc, boolean stream) throws Exception
38+
{
39+
try (JsonParser p = stream
40+
? MAPPER_CONSTRAINED.createParser(new ByteArrayInputStream(doc))
41+
: MAPPER_CONSTRAINED.createParser(doc, 0, doc.length)) {
3442
while (p.nextToken() != null) { }
3543
fail("expected StreamConstraintsException");
3644
} catch (StreamConstraintsException e) {
3745
final String msg = e.getMessage();
38-
3946
assertTrue(msg.contains("Document length ("));
4047
assertTrue(msg.contains("exceeds the maximum allowed (50000"));
4148
}

release-notes/VERSION-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Active maintainers:
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17+
2.18.6 (not yet released)
18+
19+
#649: (cbor, smile) `StreamReadConstraints.maxDocumentLength` not checked
20+
when creating parser with fixed buffer
21+
1722
2.18.5 (27-Oct-2025)
1823

1924
#599: (cbor) Unable to deserialize stringref-enabled CBOR with ignored properties

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ protected JsonParser _createParser(char[] data, int offset, int len, IOContext c
457457
@Override
458458
protected SmileParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
459459
{
460+
// [core#1548] Validate doc length up front for fixed buffers
461+
_streamReadConstraints.validateDocumentLength(len);
460462
return new SmileParserBootstrapper(ctxt, data, offset, len).constructParser(
461463
_factoryFeatures, _parserFeatures, _smileParserFeatures,
462464
_objectCodec, _byteSymbolCanonicalizer);

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/constraints/LongDocumentSmileReadTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
import com.fasterxml.jackson.core.StreamReadConstraints;
1010
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
1111

12-
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
1313
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
1414
import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper;
15-
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
1615

1716
public class LongDocumentSmileReadTest extends BaseTestForSmile
1817
{
19-
private final ObjectMapper MAPPER_VANILLA = new SmileMapper();
18+
private final SmileMapper MAPPER_VANILLA = new SmileMapper();
2019

21-
private final ObjectMapper MAPPER_CONSTRAINED = new SmileMapper(
20+
private final SmileMapper MAPPER_CONSTRAINED = new SmileMapper(
2221
SmileFactory.builder()
2322
// limit to 100kB doc reads
2423
.streamReadConstraints(StreamReadConstraints.builder()
@@ -30,13 +29,20 @@ public void testLongDocumentConstraint() throws Exception
3029
{
3130
// Need a bit longer than minimum since checking is approximate, not exact
3231
byte[] doc = createBigDoc(60_000);
33-
// Must read from `InputStream` as validation is during "loadMore()":
34-
try (JsonParser p = MAPPER_CONSTRAINED.createParser(new ByteArrayInputStream(doc))) {
32+
_testLongDocumentConstraint(doc, true);
33+
// [dataformats-binary#649] fixed buffer too
34+
_testLongDocumentConstraint(doc, false);
35+
}
36+
37+
private void _testLongDocumentConstraint(byte[] doc, boolean stream) throws Exception
38+
{
39+
try (JsonParser p = stream
40+
? MAPPER_CONSTRAINED.createParser(new ByteArrayInputStream(doc))
41+
: MAPPER_CONSTRAINED.createParser(doc, 0, doc.length)) {
3542
while (p.nextToken() != null) { }
3643
fail("expected StreamConstraintsException");
3744
} catch (StreamConstraintsException e) {
3845
final String msg = e.getMessage();
39-
4046
assertTrue(msg.contains("Document length ("));
4147
assertTrue(msg.contains("exceeds the maximum allowed (50000"));
4248
}

0 commit comments

Comments
 (0)