Skip to content

Commit a32f47b

Browse files
committed
fix: raise JSONException for truncated JSON objects
After the last value is read, offset has advanced past end and readFieldName() returns null. readFieldNameUnquote() then falls back to readFieldNameHashCodeUnquote(), which sets nameBegin = offset - 1 and skips its loop entirely because offset > end, leaving nameEnd one past the input. getFieldName() then throws StringIndexOutOfBoundsException from new String(bytes, offset, length) / substring(). Reject offset > end in readFieldNameHashCodeUnquote() for both the ASCII and UTF16 readers, so truncated input surfaces as a JSONException. Fixes #7940
1 parent 35111f3 commit a32f47b

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎core/src/main/java/com/alibaba/fastjson2/JSONReaderASCII.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ public long readFieldNameHashCodeE(int size0, int size1, int size3) {
326326
public final long readFieldNameHashCodeUnquote() {
327327
this.nameEscape = false;
328328
int offset = this.offset, end = this.end;
329+
if (offset > end) {
330+
throw error("illegal input");
331+
}
329332
final byte[] bytes = this.bytes;
330333
int ch = this.ch;
331334
this.nameBegin = offset - 1;

‎core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,9 @@ public final void nextWithoutComment() {
923923
public final long readFieldNameHashCodeUnquote() {
924924
this.nameEscape = false;
925925
int offset = this.offset, end = this.end;
926+
if (offset > end) {
927+
throw error("illegal input");
928+
}
926929
final char[] chars = this.chars;
927930
char ch = this.ch;
928931
this.nameBegin = offset - 1;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.alibaba.fastjson2.issues_7900;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.JSONException;
5+
import com.alibaba.fastjson2.JSONObject;
6+
import com.alibaba.fastjson2.JSONReader;
7+
import org.junit.jupiter.api.Tag;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.nio.charset.StandardCharsets;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
15+
@Tag("regression")
16+
public class Issue7940 {
17+
@Test
18+
public void truncatedObjectFromBytes() {
19+
String[] inputs = {
20+
"{\"a\":1",
21+
"{\"a\":1,\"b\":2",
22+
"{\"a\":1,\"b\":2,"
23+
};
24+
for (String input : inputs) {
25+
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
26+
assertThrows(JSONException.class, () -> JSON.parse(bytes), input);
27+
assertThrows(JSONException.class, () -> JSONReader.of(bytes).readAny(), input);
28+
}
29+
}
30+
31+
@Test
32+
public void truncatedObjectFromChars() {
33+
String[] inputs = {
34+
"{\"a\":1",
35+
"{\"a\":1,\"b\":2"
36+
};
37+
for (String input : inputs) {
38+
char[] chars = input.toCharArray();
39+
assertThrows(JSONException.class, () -> JSONReader.of(input).readAny(), input);
40+
assertThrows(JSONException.class, () -> JSONReader.of(chars).readAny(), input);
41+
}
42+
}
43+
44+
@Test
45+
public void completeObjectsStillParse() {
46+
assertEquals(1, JSON.parseObject("{\"a\":1}").getIntValue("a"));
47+
assertEquals(2, JSON.parseObject("{\"a\":1,\"b\":2}".getBytes(StandardCharsets.UTF_8)).getIntValue("b"));
48+
49+
JSONObject o1 = JSONReader.of("{\"a\":1}".toCharArray()).read(JSONObject.class);
50+
assertEquals(1, o1.getIntValue("a"));
51+
52+
JSONObject o2 = JSON.parseObject("{\"a\":1} \n");
53+
assertEquals(1, o2.getIntValue("a"));
54+
55+
JSONObject o3 = JSON.parseObject("{\"a\":1,\"b\":2,\"c\":3}");
56+
assertEquals(3, o3.getIntValue("c"));
57+
58+
assertEquals(0, JSON.parseObject("{}".getBytes(StandardCharsets.UTF_8)).size());
59+
}
60+
}

0 commit comments

Comments
 (0)