Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -4005,16 +4005,18 @@ public final void readNumber0() {
if (ch == 'e' || ch == 'E') {
boolean negativeExp = false;
int expValue = 0;
ch = chars[offset++];
boolean expValid = false;
ch = offset == end ? EOI : chars[offset++];

if (ch == '-') {
negativeExp = true;
ch = chars[offset++];
ch = offset == end ? EOI : chars[offset++];
} else if (ch == '+') {
ch = chars[offset++];
ch = offset == end ? EOI : chars[offset++];
}

while (ch >= '0' && ch <= '9') {
expValid = true;
valid = true;
int byteVal = (ch - '0');
expValue = expValue * 10 + byteVal;
Expand All @@ -4033,6 +4035,10 @@ public final void readNumber0() {
expValue = -expValue;
}

if (!expValid) {
throw new JSONException(info("illegal input"));
}

this.exponent = (short) expValue;
if (valueType != JSON_TYPE_BIG_DEC) {
valueType = JSON_TYPE_DEC;
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -5484,16 +5484,18 @@ public final void readNumber0() {
if (ch == 'e' || ch == 'E') {
boolean negativeExp = false;
int expValue = 0;
ch = bytes[offset++];
boolean expValid = false;
ch = offset == end ? EOI : bytes[offset++];

if (ch == '-') {
negativeExp = true;
ch = bytes[offset++];
ch = offset == end ? EOI : bytes[offset++];
} else if (ch == '+') {
ch = (char) bytes[offset++];
ch = offset == end ? EOI : bytes[offset++];
}

while (ch >= '0' && ch <= '9') {
expValid = true;
valid = true;
int byteVal = (ch - '0');
expValue = expValue * 10 + byteVal;
Expand All @@ -5512,6 +5514,10 @@ public final void readNumber0() {
expValue = -expValue;
}

if (!expValid) {
throw new JSONException(info("illegal input"));
}

this.exponent = (short) expValue;
if (valueType != JSON_TYPE_BIG_DEC) {
valueType = JSON_TYPE_DEC;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.alibaba.fastjson2.issues_7000;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Issue 7807: JSON.parse on a document that ends inside a number's exponent
* throws ArrayIndexOutOfBoundsException instead of JSONException.
*
* Regression: readNumber0 reads the exponent sign/digits without an
* end-of-input guard, so "1e", "1e-" and "1e+" (and their uppercase / fraction
* variants) overflow the byte/char buffer.
*/
public class Issue7807 {
@Test
public void testTruncatedExponentThrowsJSONException() {
String[] inputs = {
"1e", "1E",
"1e-", "1E-", "1e+", "1E+",
"1.0e", "1.0E", "1.0e-", "1.0e+",
"-1e", "-1E", "-1e-", "-1e+",
};
for (String input : inputs) {
assertThrows(JSONException.class,
() -> JSON.parse(input),
"expected JSONException for input: " + input);
}
}
}