Skip to content

Commit 44f1866

Browse files
authored
fix issue with not failing when numbers have trailing f or d (#790)
1 parent 10e5038 commit 44f1866

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,10 @@ protected JsonToken _handleInvalidNumberStart(int ch, final boolean negative, fi
17841784
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature()) && hasSign && !negative) {
17851785
reportUnexpectedNumberChar('+', "JSON spec does not allow numbers to have plus signs: enable `JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` to allow");
17861786
}
1787-
reportUnexpectedNumberChar(ch, "expected digit (0-9) to follow minus sign, for valid numeric value");
1787+
final String message = negative ?
1788+
"expected digit (0-9) to follow minus sign, for valid numeric value" :
1789+
"expected digit (0-9) for valid numeric value";
1790+
reportUnexpectedNumberChar(ch, message);
17881791
return null;
17891792
}
17901793

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2273,7 +2273,10 @@ protected JsonToken _handleInvalidNumberStart(int ch, final boolean neg, final b
22732273
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature()) && hasSign && !neg) {
22742274
reportUnexpectedNumberChar('+', "JSON spec does not allow numbers to have plus signs: enable `JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` to allow");
22752275
}
2276-
reportUnexpectedNumberChar(ch, "expected digit (0-9) to follow minus sign, for valid numeric value");
2276+
final String message = neg ?
2277+
"expected digit (0-9) to follow minus sign, for valid numeric value" :
2278+
"expected digit (0-9) for valid numeric value";
2279+
reportUnexpectedNumberChar(ch, message);
22772280
return null;
22782281
}
22792282

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2912,7 +2912,10 @@ protected JsonToken _handleInvalidNumberStart(int ch, final boolean neg, final b
29122912
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature()) && hasSign && !neg) {
29132913
reportUnexpectedNumberChar('+', "JSON spec does not allow numbers to have plus signs: enable `JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` to allow");
29142914
}
2915-
reportUnexpectedNumberChar(ch, "expected digit (0-9) to follow minus sign, for valid numeric value");
2915+
final String message = neg ?
2916+
"expected digit (0-9) to follow minus sign, for valid numeric value" :
2917+
"expected digit (0-9) for valid numeric value";
2918+
reportUnexpectedNumberChar(ch, message);
29162919
return null;
29172920
}
29182921

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,17 +1730,27 @@ protected JsonToken _finishFloatFraction() throws IOException
17301730
int outPtr = _textBuffer.getCurrentSegmentSize();
17311731

17321732
// caller guarantees at least one char; also, sign-extension not needed here
1733-
int ch;
1734-
while (((ch = _inputBuffer[_inputPtr++]) >= INT_0) && (ch <= INT_9)) {
1735-
++fractLen;
1736-
if (outPtr >= outBuf.length) {
1737-
outBuf = _textBuffer.expandCurrentSegment();
1738-
}
1739-
outBuf[outPtr++] = (char) ch;
1740-
if (_inputPtr >= _inputEnd) {
1741-
_textBuffer.setCurrentLength(outPtr);
1742-
_fractLength = fractLen;
1743-
return JsonToken.NOT_AVAILABLE;
1733+
int ch = _inputBuffer[_inputPtr++];
1734+
boolean loop = true;
1735+
while (loop) {
1736+
if (ch >= INT_0 && ch <= INT_9) {
1737+
++fractLen;
1738+
if (outPtr >= outBuf.length) {
1739+
outBuf = _textBuffer.expandCurrentSegment();
1740+
}
1741+
outBuf[outPtr++] = (char) ch;
1742+
if (_inputPtr >= _inputEnd) {
1743+
_textBuffer.setCurrentLength(outPtr);
1744+
_fractLength = fractLen;
1745+
return JsonToken.NOT_AVAILABLE;
1746+
}
1747+
ch = _inputBuffer[_inputPtr++];
1748+
} else if (ch == 'f' || ch == 'd' || ch == 'F' || ch == 'D') {
1749+
reportUnexpectedNumberChar(ch, "JSON does not support parsing numbers that have 'f' or 'd' suffixes");
1750+
} else if (ch == INT_PERIOD) {
1751+
reportUnexpectedNumberChar(ch, "Cannot parse number with more than one decimal point");
1752+
} else {
1753+
loop = false;
17441754
}
17451755
}
17461756

src/test/java/com/fasterxml/jackson/core/json/async/AsyncNonStandardNumberParsingTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ public void testNegativeHexadecimal() throws Exception
6262
}
6363
}
6464

65-
//next 2 tests do not work as expected
66-
/*
6765
public void testFloatMarker() throws Exception
6866
{
6967
final String JSON = "[ -0.123f ]";
@@ -97,7 +95,22 @@ public void testDoubleMarker() throws Exception
9795
p.close();
9896
}
9997
}
100-
*/
98+
99+
public void test2DecimalPoints() throws Exception {
100+
final String JSON = "[ -0.123.456 ]";
101+
102+
// without enabling, should get an exception
103+
AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);
104+
assertToken(JsonToken.START_ARRAY, p.nextToken());
105+
try {
106+
p.nextToken();
107+
fail("Expected exception");
108+
} catch (Exception e) {
109+
verifyException(e, "Unexpected character ('.'");
110+
} finally {
111+
p.close();
112+
}
113+
}
101114

102115
/**
103116
* The format ".NNN" (as opposed to "0.NNN") is not valid JSON, so this should fail

src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public void testDoubleMarker() throws Exception {
8080
}
8181
}
8282

83+
public void test2DecimalPoints() throws Exception {
84+
for (int mode : ALL_MODES) {
85+
try (JsonParser p = createParser(mode, " -0.123.456 ")) {
86+
p.nextToken();
87+
fail("Should not pass");
88+
} catch (JsonParseException e) {
89+
verifyException(e, "Unexpected character ('.'");
90+
}
91+
}
92+
}
93+
8394
/**
8495
* The format ".NNN" (as opposed to "0.NNN") is not valid JSON, so this should fail
8596
*/

0 commit comments

Comments
 (0)