Skip to content

Commit 0659e8c

Browse files
committed
Fix #240
1 parent db7bffa commit 0659e8c

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

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

+22-21
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,9 @@ public JsonToken nextToken() throws IOException
591591
// also: clear any data retained so far
592592
_binaryValue = null;
593593

594-
/* First: need to keep track of lengths of defined-length Arrays and
595-
* Objects (to materialize END_ARRAY/END_OBJECT as necessary);
596-
* as well as handle names for Object entries.
597-
*/
594+
// First: need to keep track of lengths of defined-length Arrays and
595+
// Objects (to materialize END_ARRAY/END_OBJECT as necessary);
596+
// as well as handle names for Object entries.
598597
if (_parsingContext.inObject()) {
599598
if (_currToken != JsonToken.FIELD_NAME) {
600599
_tagValue = -1;
@@ -3422,32 +3421,34 @@ protected void _closeInput() throws IOException {
34223421

34233422
@Override
34243423
protected void _handleEOF() throws JsonParseException {
3425-
if (!_parsingContext.inRoot()) {
3426-
String marker = _parsingContext.inArray() ? "Array" : "Object";
3427-
_reportInvalidEOF(String.format(
3428-
": expected close marker for %s (start marker at %s)",
3429-
marker,
3430-
_parsingContext.getStartLocation(_ioContext.getSourceReference())),
3431-
null);
3424+
if (_parsingContext.inRoot()) {
3425+
return;
34323426
}
3427+
String marker = _parsingContext.inArray() ? "Array" : "Object";
3428+
_reportInvalidEOF(String.format(
3429+
": expected close marker for %s (start marker at %s)",
3430+
marker,
3431+
_parsingContext.getStartLocation(_ioContext.getSourceReference())),
3432+
null);
34333433
}
34343434

3435-
/*
3436-
/**********************************************************
3437-
/* Internal methods, error handling, reporting
3438-
/**********************************************************
3439-
*/
3440-
34413435
protected JsonToken _handleCBOREOF() throws IOException {
3442-
/* NOTE: here we can and should close input, release buffers,
3443-
* since this is "hard" EOF, not a boundary imposed by
3444-
* header token.
3445-
*/
3436+
// NOTE: here we can and should close input, release buffers, since
3437+
// this is "hard" EOF, not a boundary imposed by header token.
34463438
_tagValue = -1;
34473439
close();
3440+
// 30-Jan-2021, tatu: But also MUST verify that end-of-content is actually
3441+
// allowed (see [dataformats-binary#240] for example)
3442+
_handleEOF();
34483443
return (_currToken = null);
34493444
}
34503445

3446+
/*
3447+
/**********************************************************
3448+
/* Internal methods, error handling, reporting
3449+
/**********************************************************
3450+
*/
3451+
34513452
protected void _invalidToken(int ch) throws JsonParseException {
34523453
ch &= 0xFF;
34533454
if (ch == 0xFF) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 ParseInvalidArray240Test extends CBORTestBase
10+
{
11+
private final CBORFactory F = cborFactory();
12+
13+
// [dataformats-binary#240]
14+
public void test1ByteIncompleteArray() 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: expected close marker for Array");
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)