Skip to content

Commit 6d9f5b8

Browse files
authored
Add test to show hexadecimal not supported - but fix issue in UTF8DataInputJsonParser (#786)
1 parent 10a9026 commit 6d9f5b8

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1051,11 +1051,13 @@ protected JsonToken _parseUnsignedNumber(int c) throws IOException
10511051
int outPtr;
10521052

10531053
// One special case: if first char is 0, must not be followed by a digit.
1054-
// Gets bit tricky as we only want to retain 0 if it's the full value
1054+
// Gets a bit tricky as we only want to retain 0 if it's the full value
10551055
if (c == INT_0) {
10561056
c = _handleLeadingZeroes();
10571057
if (c <= INT_9 && c >= INT_0) { // skip if followed by digit
10581058
outPtr = 0;
1059+
} else if (c == 'x' || c == 'X') {
1060+
return _handleInvalidNumberStart(c, false);
10591061
} else {
10601062
outBuf[0] = '0';
10611063
outPtr = 1;

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

+61-3
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,77 @@ protected JsonFactory jsonFactory() {
2020
return JSON_F;
2121
}
2222

23+
/**
24+
* The format "0xc0ffee" is not valid JSON, so this should fail
25+
*/
26+
public void testHexadecimal() throws Exception {
27+
for (int mode : ALL_MODES) {
28+
try (JsonParser p = createParser(mode, " 0xc0ffee ")) {
29+
p.nextToken();
30+
fail("Should not pass");
31+
} catch (JsonParseException e) {
32+
verifyException(e, "Unexpected character ('x'");
33+
}
34+
}
35+
}
36+
37+
public void testHexadecimalBigX() throws Exception {
38+
for (int mode : ALL_MODES) {
39+
try (JsonParser p = createParser(mode, " 0XC0FFEE ")) {
40+
p.nextToken();
41+
fail("Should not pass");
42+
} catch (JsonParseException e) {
43+
verifyException(e, "Unexpected character ('x'");
44+
}
45+
}
46+
}
47+
48+
public void testNegativeHexadecimal() throws Exception {
49+
for (int mode : ALL_MODES) {
50+
try (JsonParser p = createParser(mode, " -0xc0ffee ")) {
51+
p.nextToken();
52+
fail("Should not pass");
53+
} catch (JsonParseException e) {
54+
verifyException(e, "Unexpected character ('x'");
55+
}
56+
}
57+
}
58+
59+
//JSON does not allow numbers to have f or d suffixes
60+
public void testFloatMarker() throws Exception {
61+
for (int mode : ALL_MODES) {
62+
try (JsonParser p = createParser(mode, " -0.123f ")) {
63+
p.nextToken();
64+
fail("Should not pass");
65+
} catch (JsonParseException e) {
66+
verifyException(e, "Unexpected character ('f'");
67+
}
68+
}
69+
}
70+
71+
//JSON does not allow numbers to have f or d suffixes
72+
public void testDoubleMarker() throws Exception {
73+
for (int mode : ALL_MODES) {
74+
try (JsonParser p = createParser(mode, " -0.123d ")) {
75+
p.nextToken();
76+
fail("Should not pass");
77+
} catch (JsonParseException e) {
78+
verifyException(e, "Unexpected character ('d'");
79+
}
80+
}
81+
}
82+
2383
/**
2484
* The format ".NNN" (as opposed to "0.NNN") is not valid JSON, so this should fail
2585
*/
2686
public void testLeadingDotInDecimal() throws Exception {
2787
for (int mode : ALL_MODES) {
28-
JsonParser p = createParser(mode, " .123 ");
29-
try {
88+
try (JsonParser p = createParser(mode, " .123 ")) {
3089
p.nextToken();
3190
fail("Should not pass");
3291
} catch (JsonParseException e) {
3392
verifyException(e, "Unexpected character ('.'");
3493
}
35-
p.close();
3694
}
3795
}
3896

0 commit comments

Comments
 (0)