Skip to content

Commit 622c65b

Browse files
committed
Fix #287
1 parent 40ab0f8 commit 622c65b

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,7 @@ private final int _nextByte() throws IOException {
24152415
return _inputBuffer[_inputPtr++];
24162416
}
24172417

2418+
// NOTE! ALWAYS called for non-first byte of multi-byte UTF-8 code point
24182419
private final int _nextChunkedByte() throws IOException {
24192420
int inPtr = _inputPtr;
24202421

@@ -2427,6 +2428,7 @@ private final int _nextChunkedByte() throws IOException {
24272428
return ch;
24282429
}
24292430

2431+
// NOTE! ALWAYS called for non-first byte of multi-byte UTF-8 code point
24302432
private final int _nextChunkedByte2() throws IOException
24312433
{
24322434
// two possibilities: either end of buffer (in which case, just load more),
@@ -2449,10 +2451,18 @@ private final int _nextChunkedByte2() throws IOException
24492451
}
24502452
int len = _decodeChunkLength(CBORConstants.MAJOR_TYPE_TEXT);
24512453
// not actually acceptable if we got a split character
2452-
if (len < 0) {
2454+
// 29-Jun-2021, tatu: As per CBOR spec:
2455+
// "Note that this implies that the bytes of a single UTF-8 character cannot be
2456+
// spread between chunks: a new chunk can only be started at a character boundary."
2457+
// -> 0-length chunk not allowed either
2458+
if (len <= 0) {
24532459
_reportInvalidEOF(": chunked Text ends with partial UTF-8 character",
24542460
JsonToken.VALUE_STRING);
24552461
}
2462+
if (_inputPtr >= _inputEnd) { // Must have at least one byte to return
2463+
loadMoreGuaranteed();
2464+
}
2465+
24562466
int end = _inputPtr + len;
24572467
if (end <= _inputEnd) { // all within buffer
24582468
_chunkLeft = 0;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.dataformat.cbor.fuzz;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.JsonToken;
5+
import com.fasterxml.jackson.core.io.JsonEOFException;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
10+
11+
public class Fuzz32912ChunkedTextTest extends CBORTestBase
12+
{
13+
private final ObjectMapper MAPPER = cborMapper();
14+
15+
public void testInvalidShortText() throws Exception
16+
{
17+
final byte[] input = new byte[] {
18+
0x7F, 0x61,
19+
(byte) 0xF3, 0x61
20+
};
21+
22+
try (JsonParser p = MAPPER.createParser(input)) {
23+
// Won't fail immediately
24+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
25+
try {
26+
String str = p.getText();
27+
fail("Should not get String value but exception, got: ["+str+"]");
28+
} catch (JsonEOFException e) {
29+
verifyException(e, "Unexpected end-of-input in VALUE_STRING");
30+
}
31+
}
32+
}
33+
}

release-notes/VERSION-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Modules:
1010
=== Releases ===
1111
------------------------------------------------------------------------
1212

13+
2.12.4 (not yet released)
14+
15+
#287: (cbor) Uncaught exception in CBORParser._nextChunkedByte2 (by ossfuzzer)
16+
1317
2.12.3 (12-Apr-2021)
1418

1519
#257: (smile) Uncaught validation problem wrt Smile "BigDecimal" type

0 commit comments

Comments
 (0)