Skip to content

Commit

Permalink
Bit more work for #236 and #240
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 30, 2021
1 parent c40ef05 commit ab4f329
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public JsonToken nextToken() throws IOException
_streamReadContext = _streamReadContext.getParent();
return (_currToken = JsonToken.END_OBJECT);
}
return (_currToken = _decodeFieldName());
return (_currToken = _decodePropertyName());
}
} else {
if (!_streamReadContext.expectMoreValues()) {
Expand Down Expand Up @@ -1270,7 +1270,7 @@ public String nextTextValue() throws IOException
_currToken = JsonToken.END_OBJECT;
return null;
}
_currToken = _decodeFieldName();
_currToken = _decodePropertyName();
return null;
}
} else {
Expand Down Expand Up @@ -2557,7 +2557,7 @@ protected byte[] _finishLongContiguousBytes(final int expLen) throws IOException
}
}

protected final JsonToken _decodeFieldName() throws IOException
protected final JsonToken _decodePropertyName() throws IOException
{
if (_inputPtr >= _inputEnd) {
loadMoreGuaranteed();
Expand Down Expand Up @@ -3427,12 +3427,35 @@ protected void _handleEOF() throws JsonParseException {
if (_streamReadContext.inRoot()) {
return;
}
String marker = _streamReadContext.inArray() ? "Array" : "Object";
_reportInvalidEOF(String.format(
": expected close marker for %s (start marker at %s)",
marker,
_streamReadContext.getStartLocation(_ioContext.getSourceReference())),
null);
// Ok; end-marker or fixed-length Array/Object?
final JsonLocation loc = _streamReadContext.getStartLocation(_ioContext.getSourceReference());
final String startLocDesc = (loc == null) ? "[N/A]" : loc.sourceDescription();
if (_streamReadContext.hasExpectedLength()) { // specific length
final int expMore = _streamReadContext.getRemainingExpectedLength();
if (_streamReadContext.inArray()) {
_reportInvalidEOF(String.format(
" in Array value: expected %d more elements (start token at %s)",
expMore, startLocDesc),
null);
} else {
_reportInvalidEOF(String.format(
" in Object value: expected %d more properties (start token at %s)",
expMore, startLocDesc),
null);
}
} else {
if (_streamReadContext.inArray()) {
_reportInvalidEOF(String.format(
" in Array value: expected an element or close marker (0xFF) (start token at %s)",
startLocDesc),
null);
} else {
_reportInvalidEOF(String.format(
" in Object value: expected a property or close marker (0xFF) (start token at %s)",
startLocDesc),
null);
}
}
}

// Was "_handleCBOREOF()" before 2.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public CBORReadContext createChildObjectContext(int expEntryCount)
public int getRemainingExpectedLength() {
int diff = _expEntryCount - _index;
// Negative values would occur when expected count is -1
return Math.max(0, -diff);
return Math.max(0, diff);
}

public boolean acceptsBreakMarker() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.fasterxml.jackson.dataformat.cbor.parse;

import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;

public class ParseIncompleteArray240Test extends CBORTestBase
{
private final CBORFactory F = cborFactory();

// [dataformats-binary#240]
public void testIncompleteFixedSizeArray() throws Exception
{
final byte[] input = { (byte) 0x84 };
try (CBORParser p = cborParser(F, input)) {
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Should NOT pass");
} catch (StreamReadException e) {
verifyException(e, "Unexpected end-of-input in Array value: expected 4 more");
}
}
}

public void testIncompleteMarkerBasedArray() throws Exception
{
final byte[] input = { (byte) 0x9F };
try (CBORParser p = cborParser(F, input)) {
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Should NOT pass");
} catch (StreamReadException e) {
verifyException(e, "Unexpected end-of-input in Array value: expected an element or ");
}
}
}

// And might as well do the same for Objects too
/*
public void testIncompleteFixedSizeObject() throws Exception
{
final byte[] input = { (byte) 0xA3 };
try (CBORParser p = cborParser(F, input)) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
try {
p.nextToken();
fail("Should NOT pass");
} catch (StreamReadException e) {
e.printStackTrace();
verifyException(e, "Unexpected end-of-input in Object value: expected 3 more");
}
}
}
public void testIncompleteMarkerBasedObject() throws Exception
{
final byte[] input = { (byte) 0xBF };
try (CBORParser p = cborParser(F, input)) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
try {
p.nextToken();
fail("Should NOT pass");
} catch (StreamReadException e) {
verifyException(e, "Unexpected end-of-input in Object value: expected an element or ");
}
}
}
*/
}

This file was deleted.

0 comments on commit ab4f329

Please sign in to comment.