diff --git a/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java b/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java index d40df8bac..c686ef250 100644 --- a/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java +++ b/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java @@ -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()) { @@ -1270,7 +1270,7 @@ public String nextTextValue() throws IOException _currToken = JsonToken.END_OBJECT; return null; } - _currToken = _decodeFieldName(); + _currToken = _decodePropertyName(); return null; } } else { @@ -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(); @@ -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 diff --git a/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORReadContext.java b/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORReadContext.java index 868b6d069..e490c04d6 100644 --- a/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORReadContext.java +++ b/cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORReadContext.java @@ -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() { diff --git a/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseIncompleteArray240Test.java b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseIncompleteArray240Test.java new file mode 100644 index 000000000..3cf3ecb25 --- /dev/null +++ b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseIncompleteArray240Test.java @@ -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 "); + } + } + } +*/ +} diff --git a/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseInvalidArray240Test.java b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseInvalidArray240Test.java deleted file mode 100644 index c98af6802..000000000 --- a/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseInvalidArray240Test.java +++ /dev/null @@ -1,27 +0,0 @@ -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 ParseInvalidArray240Test extends CBORTestBase -{ - private final CBORFactory F = cborFactory(); - - // [dataformats-binary#240] - public void test1ByteIncompleteArray() 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: expected close marker for Array"); - } - } - } -}