Skip to content

Commit ab4f329

Browse files
committed
Bit more work for #236 and #240
1 parent c40ef05 commit ab4f329

File tree

4 files changed

+106
-37
lines changed

4 files changed

+106
-37
lines changed

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

+32-9
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ public JsonToken nextToken() throws IOException
605605
_streamReadContext = _streamReadContext.getParent();
606606
return (_currToken = JsonToken.END_OBJECT);
607607
}
608-
return (_currToken = _decodeFieldName());
608+
return (_currToken = _decodePropertyName());
609609
}
610610
} else {
611611
if (!_streamReadContext.expectMoreValues()) {
@@ -1270,7 +1270,7 @@ public String nextTextValue() throws IOException
12701270
_currToken = JsonToken.END_OBJECT;
12711271
return null;
12721272
}
1273-
_currToken = _decodeFieldName();
1273+
_currToken = _decodePropertyName();
12741274
return null;
12751275
}
12761276
} else {
@@ -2557,7 +2557,7 @@ protected byte[] _finishLongContiguousBytes(final int expLen) throws IOException
25572557
}
25582558
}
25592559

2560-
protected final JsonToken _decodeFieldName() throws IOException
2560+
protected final JsonToken _decodePropertyName() throws IOException
25612561
{
25622562
if (_inputPtr >= _inputEnd) {
25632563
loadMoreGuaranteed();
@@ -3427,12 +3427,35 @@ protected void _handleEOF() throws JsonParseException {
34273427
if (_streamReadContext.inRoot()) {
34283428
return;
34293429
}
3430-
String marker = _streamReadContext.inArray() ? "Array" : "Object";
3431-
_reportInvalidEOF(String.format(
3432-
": expected close marker for %s (start marker at %s)",
3433-
marker,
3434-
_streamReadContext.getStartLocation(_ioContext.getSourceReference())),
3435-
null);
3430+
// Ok; end-marker or fixed-length Array/Object?
3431+
final JsonLocation loc = _streamReadContext.getStartLocation(_ioContext.getSourceReference());
3432+
final String startLocDesc = (loc == null) ? "[N/A]" : loc.sourceDescription();
3433+
if (_streamReadContext.hasExpectedLength()) { // specific length
3434+
final int expMore = _streamReadContext.getRemainingExpectedLength();
3435+
if (_streamReadContext.inArray()) {
3436+
_reportInvalidEOF(String.format(
3437+
" in Array value: expected %d more elements (start token at %s)",
3438+
expMore, startLocDesc),
3439+
null);
3440+
} else {
3441+
_reportInvalidEOF(String.format(
3442+
" in Object value: expected %d more properties (start token at %s)",
3443+
expMore, startLocDesc),
3444+
null);
3445+
}
3446+
} else {
3447+
if (_streamReadContext.inArray()) {
3448+
_reportInvalidEOF(String.format(
3449+
" in Array value: expected an element or close marker (0xFF) (start token at %s)",
3450+
startLocDesc),
3451+
null);
3452+
} else {
3453+
_reportInvalidEOF(String.format(
3454+
" in Object value: expected a property or close marker (0xFF) (start token at %s)",
3455+
startLocDesc),
3456+
null);
3457+
}
3458+
}
34363459
}
34373460

34383461
// Was "_handleCBOREOF()" before 2.13

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public CBORReadContext createChildObjectContext(int expEntryCount)
138138
public int getRemainingExpectedLength() {
139139
int diff = _expEntryCount - _index;
140140
// Negative values would occur when expected count is -1
141-
return Math.max(0, -diff);
141+
return Math.max(0, diff);
142142
}
143143

144144
public boolean acceptsBreakMarker() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fasterxml.jackson.dataformat.cbor.parse;
2+
3+
import com.fasterxml.jackson.core.JsonToken;
4+
import com.fasterxml.jackson.core.exc.StreamReadException;
5+
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
6+
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
7+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
8+
9+
public class ParseIncompleteArray240Test extends CBORTestBase
10+
{
11+
private final CBORFactory F = cborFactory();
12+
13+
// [dataformats-binary#240]
14+
public void testIncompleteFixedSizeArray() throws Exception
15+
{
16+
final byte[] input = { (byte) 0x84 };
17+
try (CBORParser p = cborParser(F, input)) {
18+
assertToken(JsonToken.START_ARRAY, p.nextToken());
19+
try {
20+
p.nextToken();
21+
fail("Should NOT pass");
22+
} catch (StreamReadException e) {
23+
verifyException(e, "Unexpected end-of-input in Array value: expected 4 more");
24+
}
25+
}
26+
}
27+
28+
public void testIncompleteMarkerBasedArray() throws Exception
29+
{
30+
final byte[] input = { (byte) 0x9F };
31+
try (CBORParser p = cborParser(F, input)) {
32+
assertToken(JsonToken.START_ARRAY, p.nextToken());
33+
try {
34+
p.nextToken();
35+
fail("Should NOT pass");
36+
} catch (StreamReadException e) {
37+
verifyException(e, "Unexpected end-of-input in Array value: expected an element or ");
38+
}
39+
}
40+
}
41+
42+
// And might as well do the same for Objects too
43+
/*
44+
public void testIncompleteFixedSizeObject() throws Exception
45+
{
46+
final byte[] input = { (byte) 0xA3 };
47+
try (CBORParser p = cborParser(F, input)) {
48+
assertToken(JsonToken.START_OBJECT, p.nextToken());
49+
try {
50+
p.nextToken();
51+
fail("Should NOT pass");
52+
} catch (StreamReadException e) {
53+
e.printStackTrace();
54+
verifyException(e, "Unexpected end-of-input in Object value: expected 3 more");
55+
}
56+
}
57+
}
58+
59+
public void testIncompleteMarkerBasedObject() throws Exception
60+
{
61+
final byte[] input = { (byte) 0xBF };
62+
try (CBORParser p = cborParser(F, input)) {
63+
assertToken(JsonToken.START_OBJECT, p.nextToken());
64+
try {
65+
p.nextToken();
66+
fail("Should NOT pass");
67+
} catch (StreamReadException e) {
68+
verifyException(e, "Unexpected end-of-input in Object value: expected an element or ");
69+
}
70+
}
71+
}
72+
*/
73+
}

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseInvalidArray240Test.java

-27
This file was deleted.

0 commit comments

Comments
 (0)