Skip to content

Commit dffb2c9

Browse files
authored
Fix issue 458: Add null checking (#459)
1 parent c473e1a commit dffb2c9

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,13 @@ protected void _releaseBuffers() throws IOException
777777
@Override
778778
public JsonToken nextToken() throws IOException
779779
{
780-
_numTypesValid = NR_UNKNOWN;
781780
// For longer tokens (text, binary), we'll only read when requested
782781
if (_tokenIncomplete) {
783782
_skipIncomplete();
784783
}
785784
_tokenInputTotal = _currInputProcessed + _inputPtr;
786785
// also: clear any data retained so far
786+
_numTypesValid = NR_UNKNOWN;
787787
_binaryValue = null;
788788

789789
// First: need to keep track of lengths of defined-length Arrays and
@@ -1112,6 +1112,9 @@ protected JsonToken _handleTaggedBinary(TagList tags) throws IOException
11121112
} else {
11131113
// 12-May-2016, tatu: Since that's all we know, let's otherwise
11141114
// just return default Binary data marker
1115+
// 16-Jan-2024, tatu: Esoteric edge case where we have marked
1116+
// `int` as being tokenized
1117+
_numTypesValid = NR_UNKNOWN;
11151118
return (_currToken = JsonToken.VALUE_EMBEDDED_OBJECT);
11161119
}
11171120

@@ -1558,7 +1561,7 @@ public String nextFieldName() throws IOException
15581561
return name;
15591562
}
15601563
// otherwise just fall back to default handling; should occur rarely
1561-
return (nextToken() == JsonToken.FIELD_NAME) ? getCurrentName() : null;
1564+
return (nextToken() == JsonToken.FIELD_NAME) ? currentName() : null;
15621565
}
15631566

15641567
// 06-Apr-2023, tatu: Before Jackson 2.15, we had optimized variant, but
@@ -2224,6 +2227,11 @@ protected void convertNumberToBigDecimal() throws IOException
22242227
// Let's parse from String representation, to avoid rounding errors that
22252228
//non-decimal floating operations would incur
22262229
final String text = getText();
2230+
// 16-Jan-2024, tatu: OSS-Fuzz managed to trigger this; let's fail
2231+
// explicitly
2232+
if (text == null) {
2233+
_throwInternal();
2234+
}
22272235
streamReadConstraints().validateFPLength(text.length());
22282236
_numberBigDecimal = NumberInput.parseBigDecimal(
22292237
text, isEnabled(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.exc.StreamReadException;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
10+
11+
public class CBORFuzz458_65768_NPETest extends CBORTestBase
12+
{
13+
private final ObjectMapper MAPPER = cborMapper();
14+
15+
public void testInvalidText() throws Exception
16+
{
17+
final byte[] input = readResource("/data/clusterfuzz-cbor-65768.cbor");
18+
try (JsonParser p = MAPPER.createParser(input)) {
19+
try {
20+
assertNull(p.nextTextValue());
21+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.currentToken());
22+
assertEquals(0, p.getIntValue());
23+
assertNull(p.nextTextValue());
24+
assertNull(p.nextTextValue());
25+
assertNull(p.nextTextValue());
26+
assertNull(p.nextTextValue());
27+
assertNull(p.nextTextValue());
28+
assertNull(p.nextTextValue());
29+
assertNull(p.nextTextValue());
30+
assertNull(p.nextTextValue());
31+
assertNull(p.nextTextValue());
32+
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.currentToken());
33+
p.getFloatValue();
34+
p.getDecimalValue();
35+
fail("Should not reach here (invalid input)");
36+
} catch (StreamReadException e) {
37+
verifyException(e, "Current token (VALUE_EMBEDDED_OBJECT) not numeric");
38+
}
39+
}
40+
}
41+
}
Binary file not shown.

release-notes/CREDITS-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,5 @@ Arthur Chan (@arthurscchan)
303303
(2.17.0)
304304
* Contributed #451: (cbor) `IndexOutOfBoundsException` in `CBORParser` for invalid input
305305
(2.17.0)
306+
* Contributed #458: (cbor) Unexpected NullPointerException in `CBORParser`
307+
(2.17.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Active maintainers:
3535
(fix contributed by Arthur C)
3636
#451: (cbor) `IndexOutOfBoundsException` in `CBORParser` for invalid input
3737
(fix contributed by Arthur C)
38+
#458: (cbor) Unexpected NullPointerException in `CBORParser`
39+
(fix contributed by Arthur C)
3840
- (ion) Update `com.amazon.ion:ion-java` to 1.11.0 (from 1.10.5)
3941

4042
2.16.1 (24-Dec-2023)

0 commit comments

Comments
 (0)