Repository navigation
Conversation
wylovelyi
force-pushed
the
fix-issue-7940
branch
from
November 4, 2026 06:39
a32f47b to
cf8a903
Compare
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
wylovelyi
force-pushed
the
fix-issue-7940
branch
from
November 4, 2026 06:46
cf8a903 to
1d7a7b0
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does / why we need it?
A truncated JSON object crashes with
StringIndexOutOfBoundsExceptioninstead ofJSONException: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 catchJSONException; anIndexOutOfBoundsExceptionescapes that contract and looks like a library defect rather than bad input.Root cause
After the last value is read,
this.offsethas moved pastend(offset == end + 1), and the object reader asks for the next field name.readFieldName()returnsnull, soreadFieldNameUnquote()falls back toreadFieldNameHashCodeUnquote():The loop body never runs, so
nameEndkeeps 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 toJSONReaderASCII, while any non-ASCII byte goes toJSONReaderUTF8.JSONReader.of(String)/of(char[])go toJSONReaderUTF16. Fixing only the ASCII and UTF16 readers leaves the non-ASCIIbyte[]path crashing, so all three are covered here.Summary of your change
Reject the already-past-the-end state at the start of
readFieldNameHashCodeUnquote()inJSONReaderASCII,JSONReaderUTF16andJSONReaderUTF8(3 lines each, identical):offset == endstays valid (it is the normal "EOI already consumed" state), onlyoffset > endis rejected.Validation
Issue7940covers truncated objects in three shapes:{"a":1,{"a":1,"b":2,{"a":1,"b":2,viaJSON.parse(byte[])/JSONReader.of(byte[]){"中":1,{"中":1,"文":2,{"中":1,"文":2,viaJSON.parse(byte[]),JSONReader.of(byte[]),JSONReader.of(ByteBuffer, UTF_8)andJSONReader.of(InputStream, UTF_8)char[]/Stringinput, both ASCII and non-ASCII, viaJSONReader.of(...)JSONException; before the fix,{"中":1threwStringIndexOutOfBoundsException: offset 8, count 1, length 8.{"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' testpasses (244 tests, JDK 17).Fixes #7940
Please indicate you've done the following: