Skip to content

Commit 3e0e853

Browse files
committed
Fix #616
1 parent f5209c3 commit 3e0e853

File tree

8 files changed

+83
-43
lines changed

8 files changed

+83
-43
lines changed

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,8 @@ Volkan Yazıcı (vy@github)
216216
* Reported #609: (partial fix) `FilteringGeneratorDelegate` does not handle
217217
`writeString(Reader, int)`
218218
(2.10.4 [partial], 2.11.0 [full fix])
219+
220+
Justin Liu (jusliu@github)
221+
* Reported #616: Parsing JSON with `ALLOW_MISSING_VALUE` enabled results in endless stream
222+
of `VALUE_NULL` tokens
223+
(2.10.5)

release-notes/VERSION-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ JSON library.
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17+
2.10.5 (not yet released)
18+
19+
#616: Parsing JSON with `ALLOW_MISSING_VALUE` enabled results in endless stream
20+
of `VALUE_NULL` tokens
21+
(reported by Justin L)
22+
1723
2.10.4 (03-May-2020)
1824

1925
#605: Handle case when system property access is restricted

src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -1132,17 +1132,20 @@ private final JsonToken _nextTokenNotInObject(int i) throws IOException
11321132
return (_currToken = _parsePosNumber(i));
11331133
/*
11341134
* This check proceeds only if the Feature.ALLOW_MISSING_VALUES is enabled
1135-
* The Check is for missing values. Incase of missing values in an array, the next token will be either ',' or ']'.
1135+
* The Check is for missing values. In case of missing values in an array, the next token will be either ',' or ']'.
11361136
* This case, decrements the already incremented _inputPtr in the buffer in case of comma(,)
11371137
* so that the existing flow goes back to checking the next token which will be comma again and
11381138
* it continues the parsing.
11391139
* Also the case returns NULL as current token in case of ',' or ']'.
11401140
*/
1141+
// case ']': // 11-May-2020, tatu: related to [core#616], this should never be reached
11411142
case ',':
1142-
case ']':
1143-
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
1144-
--_inputPtr;
1145-
return (_currToken = JsonToken.VALUE_NULL);
1143+
// 11-May-2020, tatu: [core#616] No commas in root level
1144+
if (!_parsingContext.inRoot()) {
1145+
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
1146+
--_inputPtr;
1147+
return (_currToken = JsonToken.VALUE_NULL);
1148+
}
11461149
}
11471150
}
11481151
return (_currToken = _handleOddValue(i));
@@ -1881,9 +1884,12 @@ protected JsonToken _handleOddValue(int i) throws IOException
18811884
}
18821885
// fall through
18831886
case ',':
1884-
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
1885-
--_inputPtr;
1886-
return JsonToken.VALUE_NULL;
1887+
// 11-May-2020, tatu: [core#616] No commas in root level
1888+
if (!_parsingContext.inRoot()) {
1889+
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
1890+
--_inputPtr;
1891+
return JsonToken.VALUE_NULL;
1892+
}
18871893
}
18881894
break;
18891895
case 'N':

src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -2027,10 +2027,13 @@ protected JsonToken _handleUnexpectedValue(int c)
20272027
/* !!! TODO: 08-May-2016, tatu: To support `Feature.ALLOW_MISSING_VALUES` would
20282028
* need handling here...
20292029
*/
2030-
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
2030+
// 11-May-2020, tatu: [core#616] No commas in root level
2031+
if (!_parsingContext.inRoot()) {
2032+
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
20312033
// _inputPtr--;
2032-
_nextByte = c;
2033-
return JsonToken.VALUE_NULL;
2034+
_nextByte = c;
2035+
return JsonToken.VALUE_NULL;
2036+
}
20342037
}
20352038
// fall through
20362039
case '}':

src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -2611,9 +2611,12 @@ protected JsonToken _handleUnexpectedValue(int c) throws IOException
26112611
// 28-Mar-2016: [core#116]: If Feature.ALLOW_MISSING_VALUES is enabled
26122612
// we may allow "missing values", that is, encountering a trailing
26132613
// comma or closing marker where value would be expected
2614-
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
2615-
--_inputPtr;
2616-
return JsonToken.VALUE_NULL;
2614+
// 11-May-2020, tatu: [core#616] No commas in root level
2615+
if (!_parsingContext.inRoot()) {
2616+
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
2617+
--_inputPtr;
2618+
return JsonToken.VALUE_NULL;
2619+
}
26172620
}
26182621
// fall through
26192622
case '}':

src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingJsonParser.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,12 @@ protected JsonToken _startUnexpectedValue(boolean leadingComma, int ch) throws I
904904
// 28-Mar-2016: [core#116]: If Feature.ALLOW_MISSING_VALUES is enabled
905905
// we may allow "missing values", that is, encountering a trailing
906906
// comma or closing marker where value would be expected
907-
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
908-
--_inputPtr;
909-
return _valueComplete(JsonToken.VALUE_NULL);
907+
// 11-May-2020, tatu: [core#616] No commas in root level
908+
if (!_parsingContext.inRoot()) {
909+
if ((_features & FEAT_MASK_ALLOW_MISSING) != 0) {
910+
--_inputPtr;
911+
return _valueComplete(JsonToken.VALUE_NULL);
912+
}
910913
}
911914
// fall through
912915
case INT_RCURLY:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fasterxml.jackson.core.read;
2+
3+
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.json.JsonReadFeature;
5+
6+
public class TrailingCommas616Test extends BaseTest
7+
{
8+
final JsonFactory f = JsonFactory.builder()
9+
.enable(JsonReadFeature.ALLOW_MISSING_VALUES)
10+
.build();
11+
12+
// [core#616]
13+
public void testRootLevelComma616() throws Exception
14+
{
15+
_testRootLevel616(f, MODE_READER);
16+
}
17+
18+
public void testRootLevelComma616Bytes() throws Exception
19+
{
20+
_testRootLevel616(f, MODE_INPUT_STREAM);
21+
_testRootLevel616(f, MODE_INPUT_STREAM_THROTTLED);
22+
}
23+
24+
public void testRootLevelComma616DataInput() throws Exception
25+
{
26+
_testRootLevel616(f, MODE_DATA_INPUT);
27+
}
28+
29+
private void _testRootLevel616(JsonFactory f, int mode) throws Exception
30+
{
31+
JsonParser p = createParser(f, mode, ",");
32+
try {
33+
p.nextToken();
34+
fail("Should not pass");
35+
} catch (JsonParseException e) {
36+
verifyException(e, "Unexpected character (','");
37+
}
38+
p.close();
39+
}
40+
}

src/test/java/com/fasterxml/jackson/failing/TrailingCommas616Test.java

-26
This file was deleted.

0 commit comments

Comments
 (0)