Skip to content

Commit

Permalink
fix issue with not failing when numbers have trailing f or d (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Jul 9, 2022
1 parent 10e5038 commit 44f1866
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,10 @@ protected JsonToken _handleInvalidNumberStart(int ch, final boolean negative, fi
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature()) && hasSign && !negative) {
reportUnexpectedNumberChar('+', "JSON spec does not allow numbers to have plus signs: enable `JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` to allow");
}
reportUnexpectedNumberChar(ch, "expected digit (0-9) to follow minus sign, for valid numeric value");
final String message = negative ?
"expected digit (0-9) to follow minus sign, for valid numeric value" :
"expected digit (0-9) for valid numeric value";
reportUnexpectedNumberChar(ch, message);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2273,7 +2273,10 @@ protected JsonToken _handleInvalidNumberStart(int ch, final boolean neg, final b
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature()) && hasSign && !neg) {
reportUnexpectedNumberChar('+', "JSON spec does not allow numbers to have plus signs: enable `JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` to allow");
}
reportUnexpectedNumberChar(ch, "expected digit (0-9) to follow minus sign, for valid numeric value");
final String message = neg ?
"expected digit (0-9) to follow minus sign, for valid numeric value" :
"expected digit (0-9) for valid numeric value";
reportUnexpectedNumberChar(ch, message);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2912,7 +2912,10 @@ protected JsonToken _handleInvalidNumberStart(int ch, final boolean neg, final b
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature()) && hasSign && !neg) {
reportUnexpectedNumberChar('+', "JSON spec does not allow numbers to have plus signs: enable `JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` to allow");
}
reportUnexpectedNumberChar(ch, "expected digit (0-9) to follow minus sign, for valid numeric value");
final String message = neg ?
"expected digit (0-9) to follow minus sign, for valid numeric value" :
"expected digit (0-9) for valid numeric value";
reportUnexpectedNumberChar(ch, message);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1730,17 +1730,27 @@ protected JsonToken _finishFloatFraction() throws IOException
int outPtr = _textBuffer.getCurrentSegmentSize();

// caller guarantees at least one char; also, sign-extension not needed here
int ch;
while (((ch = _inputBuffer[_inputPtr++]) >= INT_0) && (ch <= INT_9)) {
++fractLen;
if (outPtr >= outBuf.length) {
outBuf = _textBuffer.expandCurrentSegment();
}
outBuf[outPtr++] = (char) ch;
if (_inputPtr >= _inputEnd) {
_textBuffer.setCurrentLength(outPtr);
_fractLength = fractLen;
return JsonToken.NOT_AVAILABLE;
int ch = _inputBuffer[_inputPtr++];
boolean loop = true;
while (loop) {
if (ch >= INT_0 && ch <= INT_9) {
++fractLen;
if (outPtr >= outBuf.length) {
outBuf = _textBuffer.expandCurrentSegment();
}
outBuf[outPtr++] = (char) ch;
if (_inputPtr >= _inputEnd) {
_textBuffer.setCurrentLength(outPtr);
_fractLength = fractLen;
return JsonToken.NOT_AVAILABLE;
}
ch = _inputBuffer[_inputPtr++];
} else if (ch == 'f' || ch == 'd' || ch == 'F' || ch == 'D') {
reportUnexpectedNumberChar(ch, "JSON does not support parsing numbers that have 'f' or 'd' suffixes");
} else if (ch == INT_PERIOD) {
reportUnexpectedNumberChar(ch, "Cannot parse number with more than one decimal point");
} else {
loop = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public void testNegativeHexadecimal() throws Exception
}
}

//next 2 tests do not work as expected
/*
public void testFloatMarker() throws Exception
{
final String JSON = "[ -0.123f ]";
Expand Down Expand Up @@ -97,7 +95,22 @@ public void testDoubleMarker() throws Exception
p.close();
}
}
*/

public void test2DecimalPoints() throws Exception {
final String JSON = "[ -0.123.456 ]";

// without enabling, should get an exception
AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Expected exception");
} catch (Exception e) {
verifyException(e, "Unexpected character ('.'");
} finally {
p.close();
}
}

/**
* The format ".NNN" (as opposed to "0.NNN") is not valid JSON, so this should fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public void testDoubleMarker() throws Exception {
}
}

public void test2DecimalPoints() throws Exception {
for (int mode : ALL_MODES) {
try (JsonParser p = createParser(mode, " -0.123.456 ")) {
p.nextToken();
fail("Should not pass");
} catch (JsonParseException e) {
verifyException(e, "Unexpected character ('.'");
}
}
}

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

0 comments on commit 44f1866

Please sign in to comment.