Skip to content

fix: raise JSONException for truncated JSON objects - #7941

Open
wylovelyi wants to merge 1 commit into
alibaba:mainfrom
wylovelyi:fix-issue-7940
Open

wylovelyi wants to merge 1 commit into
alibaba:mainfrom
wylovelyi:fix-issue-7940

Conversation

@wylovelyi

@wylovelyi wylovelyi commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

A truncated JSON object crashes with StringIndexOutOfBoundsException instead of JSONException:

JSON.parse("{\"a\":1".getBytes(StandardCharsets.UTF_8));
JSON.parse("{\"中\":1".getBytes(StandardCharsets.UTF_8));   // non-ASCII content picks the UTF8 reader
java.lang.StringIndexOutOfBoundsException: offset 6, count 1, length 6
    at java.base/java.lang.String.<init>(String.java:523)
    at com.alibaba.fastjson2.JSONReaderASCII.getFieldName(JSONReaderASCII.java:823)
    at com.alibaba.fastjson2.JSONReader.readFieldNameUnquote(JSONReader.java:1724)
    at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:152)

Truncated payloads are common in practice (partial network reads, incomplete files), and JSON.parse(byte[]) is the first entry point people use for them. Callers normally catch JSONException; an IndexOutOfBoundsException escapes that contract and looks like a library defect rather than bad input.

Root cause

After the last value is read, this.offset has moved past end (offset == end + 1), and the object reader asks for the next field name. readFieldName() returns null, so readFieldNameUnquote() falls back to readFieldNameHashCodeUnquote():

this.nameBegin = offset - 1;                    // == end
for (int i = 0; offset <= end; ++i) { ... }     // offset > end: body never runs

The loop body never runs, so nameEnd keeps its previous value (end + 1). getFieldName() then slices [end, end + 1) and goes one byte (or char) past the input.

The bug lives in all three readers. JSON.parse(byte[]) picks the reader by content: pure ASCII goes to JSONReaderASCII, while any non-ASCII byte goes to JSONReaderUTF8. JSONReader.of(String) / of(char[]) go to JSONReaderUTF16. Fixing only the ASCII and UTF16 readers leaves the non-ASCII byte[] path crashing, so all three are covered here.

Summary of your change

Reject the already-past-the-end state at the start of readFieldNameHashCodeUnquote() in JSONReaderASCII, JSONReaderUTF16 and JSONReaderUTF8 (3 lines each, identical):

int offset = this.offset, end = this.end;
if (offset > end) {
    throw error("illegal input");
}

offset == end stays valid (it is the normal "EOI already consumed" state), only offset > end is rejected.

Validation

  • Issue7940 covers truncated objects in three shapes:
    • ASCII: {"a":1, {"a":1,"b":2, {"a":1,"b":2, via JSON.parse(byte[]) / JSONReader.of(byte[])
    • non-ASCII: {"中":1, {"中":1,"文":2, {"中":1,"文":2, via JSON.parse(byte[]), JSONReader.of(byte[]), JSONReader.of(ByteBuffer, UTF_8) and JSONReader.of(InputStream, UTF_8)
    • char[] / String input, both ASCII and non-ASCII, via JSONReader.of(...)
  • All of the above now throw JSONException; before the fix, {"中":1 threw StringIndexOutOfBoundsException: offset 8, count 1, length 8.
  • Complete objects still parse: {"a":1}, {"a":1,"b":2}, {"a":1,"b":2,"c":3}, trailing whitespace, {}.
  • ./mvnw -pl core -Dtest='Issue7940,Issue7807,Issue7825,Issue7826,JSONReaderTest,JSONReaderTest1,JSONReaderTest2,JSONReaderUTF16Test,JSONReaderUTF8Test,UnquoteNameTest' test passes (244 tests, JDK 17).
  • Verified against the released 2.0.65 jar that the crash is pre-existing and not introduced by recent changes.

Fixes #7940

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits.
  • Considered the docs impact (none).

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 the ASCII, UTF16
and UTF8 readers. JSON.parse(byte[]) selects the reader by content, so
covering only ASCII and UTF16 leaves non-ASCII payloads crashing.

Fixes alibaba#7940

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] StringIndexOutOfBoundsException when parsing a truncated JSON object from byte[]

1 participant