Skip to content

Commit 0f31d73

Browse files
committed
Some more fixes to non-standard numbers, almost there
1 parent d737866 commit 0f31d73

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public int releaseBuffered(Writer w) throws IOException {
211211
protected char getNextChar(String eofMsg) throws IOException {
212212
return getNextChar(eofMsg, null);
213213
}
214-
214+
215215
protected char getNextChar(String eofMsg, JsonToken forToken) throws IOException {
216216
if (_inputPtr >= _inputEnd) {
217217
if (!_loadMore()) {
@@ -1576,6 +1576,7 @@ private final JsonToken _parseNumber2(boolean neg, int startPtr) throws IOExcept
15761576
int intLen = 0;
15771577
char c = (_inputPtr < _inputEnd) ? _inputBuffer[_inputPtr++]
15781578
: getNextChar("No digit following minus sign", JsonToken.VALUE_NUMBER_INT);
1579+
15791580
if (c == '0') {
15801581
c = _verifyNoLeadingZeroes();
15811582
}
@@ -1606,9 +1607,10 @@ private final JsonToken _parseNumber2(boolean neg, int startPtr) throws IOExcept
16061607
}
16071608
}
16081609

1609-
int fractLen = 0;
1610+
int fractLen = -1;
16101611
// And then see if we get other parts
16111612
if (c == '.') { // yes, fraction
1613+
fractLen = 0;
16121614
if (outPtr >= outBuf.length) {
16131615
outBuf = _textBuffer.finishCurrentSegment();
16141616
outPtr = 0;
@@ -1640,16 +1642,17 @@ private final JsonToken _parseNumber2(boolean neg, int startPtr) throws IOExcept
16401642
}
16411643
}
16421644

1643-
int expLen = 0;
1645+
int expLen = -1;
16441646
if (c == 'e' || c == 'E') { // exponent?
1647+
expLen = 0;
16451648
if (outPtr >= outBuf.length) {
16461649
outBuf = _textBuffer.finishCurrentSegment();
16471650
outPtr = 0;
16481651
}
16491652
outBuf[outPtr++] = c;
16501653
// Not optional, can require that we get one more char
16511654
c = (_inputPtr < _inputEnd) ? _inputBuffer[_inputPtr++]
1652-
: getNextChar("expected a digit for number exponent");
1655+
: getNextChar("expected a digit for number exponent", JsonToken.VALUE_NUMBER_FLOAT);
16531656
// Sign indicator?
16541657
if (c == '-' || c == '+') {
16551658
if (outPtr >= outBuf.length) {
@@ -1659,7 +1662,7 @@ private final JsonToken _parseNumber2(boolean neg, int startPtr) throws IOExcept
16591662
outBuf[outPtr++] = c;
16601663
// Likewise, non optional:
16611664
c = (_inputPtr < _inputEnd) ? _inputBuffer[_inputPtr++]
1662-
: getNextChar("expected a digit for number exponent");
1665+
: getNextChar("expected a digit for number exponent", JsonToken.VALUE_NUMBER_FLOAT);
16631666
}
16641667

16651668
exp_loop:
@@ -1690,8 +1693,14 @@ private final JsonToken _parseNumber2(boolean neg, int startPtr) throws IOExcept
16901693
}
16911694
}
16921695
_textBuffer.setCurrentLength(outPtr);
1696+
16931697
// And there we have it!
1694-
return reset(neg, intLen, fractLen, expLen);
1698+
// 26-Jun-2022, tatu: Careful here, as non-standard numbers can
1699+
// cause surprises - cannot use plain "reset()" but apply diff logic
1700+
if (fractLen < 0 && expLen < 0) { // integer
1701+
return resetInt(neg, intLen);
1702+
}
1703+
return resetFloat(neg, intLen, fractLen, expLen);
16951704
}
16961705

16971706
// Method called when we have seen one zero, and want to ensure

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testLeadingDotInDecimal() throws Exception {
3939
/*
4040
* The format "+NNN" (as opposed to "NNN") is not valid JSON, so this should fail
4141
*/
42-
public void testLeadingPlusSignInDecimal() throws Exception {
42+
public void testLeadingPlusSignInDecimalDefaultFail() throws Exception {
4343
for (int mode : ALL_MODES) {
4444
try (JsonParser p = createParser(mode, " +123 ")) {
4545
p.nextToken();
@@ -97,7 +97,7 @@ public void testTrailingDotInDecimalAllowedBytes() throws Exception {
9797

9898
public void testTrailingDotInDecimalAllowedReader() throws Exception {
9999
_testTrailingDotInDecimalAllowed(jsonFactory(), MODE_READER);
100-
// _testTrailingDotInDecimalAllowed(jsonFactory(), MODE_READER_THROTTLED);
100+
_testTrailingDotInDecimalAllowed(jsonFactory(), MODE_READER_THROTTLED);
101101
}
102102

103103
public void testLeadingPlusSignInDecimalAllowedDataInput() throws Exception {
@@ -125,7 +125,7 @@ public void testLeadingDotInNegativeDecimalAllowedBytes() throws Exception {
125125

126126
public void testLeadingDotInNegativeDecimalAllowedReader() throws Exception {
127127
_testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER);
128-
// _testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER_THROTTLED);
128+
_testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER_THROTTLED);
129129
}
130130

131131
private void _testLeadingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception

0 commit comments

Comments
 (0)