Skip to content

fix: raise JSONException for field name ending in backslash - #7939

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

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

Conversation

@wylovelyi

@wylovelyi wylovelyi commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

JSONReaderASCII.readFieldName() throws ArrayIndexOutOfBoundsException for a field name that ends with a backslash:

JSON.parse("{\"\\");   // Index 3 out of bounds for length 3

The loop guard only covers bytes[offset], while the escape branch reads bytes[offset + 1] to decide the skip length. When the backslash is the final byte, offset + 1 == end.

This is the same defect class as #7914 (bound reads on truncated numbers, comments and field names). #7914 fixed the two whitespace loops in this method, but not this lookahead. Its description mentions that #7810 no longer reproduces; #7809 still does on current main (35111f33).

Summary of your change

  • Guard the lookahead in JSONReaderASCII.readFieldName() and raise JSONException, consistent with the existing malformed-name handling.
  • The UTF8/UTF16 readers already use byteAfterEscape / charAfterEscape plus checkEscapeEnd, so only the ASCII reader needs this guard. Escaped field names are unaffected.

Validation

  • Issue7809 covers backslash-terminated field names through both input paths: {"\, {"a\ and {"a":1,"b\ as String, and {"\ / {"a\ as UTF-8 bytes. The 8-, 9- and 10-character cases also exercise the nameValue0 / nameValue1 fast paths in readFieldName().
  • Escaped and long field names still parse correctly: {"a\"b":1}, {"a\\b":1}, {"a\u0041b":1}, {"a\tb":1}, {"abcdefgh":1} through {"abcdefghij":1} (both String and bytes), plus normal {"a":1} and {}.
  • ./mvnw -pl core -Dtest=Issue7809 test passes on JDK 17.

Fixes #7809

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).

JSONReaderASCII.readFieldName() reads bytes[offset + 1] to decide the escape
length, but the loop only guards offset < end. A backslash as the final byte
read past the array and leaked ArrayIndexOutOfBoundsException instead of the
documented JSONException. Guard the lookahead, and report the same
"invalid escape character EOI" message as the UTF8/UTF16 readers.

Fixes alibaba#7809

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] ArrayIndexOutOfBoundsException on a field name ending in a backslash — JSON.parse("{\"\\")

1 participant