Skip to content

fix: bound field name escape read in JSONReaderASCII - #7922

Open
Support-itecz wants to merge 1 commit into
alibaba:mainfrom
Support-itecz:fieldname-escape-bound
Open

Support-itecz wants to merge 1 commit into
alibaba:mainfrom
Support-itecz:fieldname-escape-bound

Conversation

@Support-itecz

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

JSONReaderASCII.readFieldName scans a quoted key in a for (... ; offset < end; ...) loop that bounds bytes[offset], but on a \ escape it reads bytes[offset + 1] without checking that against end. When the \ is the last byte of a truncated input the read lands on bytes[end], so JSON.parseObject(String) and JSON.parseObject(byte[]) raise ArrayIndexOutOfBoundsException instead of JSONException. Trigger: JSON.parseObject("{\"\\"), or any key cut at a backslash such as {"ab\.

The sibling readFieldName in JSONReaderUTF8 and JSONReaderUTF16 already bound the escape through byteAfterEscape/charAfterEscape and checkEscapeEnd; the ASCII reader was the only one missing the check, and the value-string readers in the same class already throw invalid escape character EOI on the same truncation.

Summary of your change

  • bound offset + 1 against end before the escape read in JSONReaderASCII.readFieldName, throwing the invalid escape character EOI JSONException the other escape sites in the file already use
  • FieldNameEscapeTruncationTest covers the truncated-escape key for String and byte[] input plus a valid escaped-key round-trip

Sits with the recent truncated-input bounds work on readNumber0, skipComment and the readFieldName whitespace path.

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 specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Downgraded from Approve to Comment: PR metadata unavailable — could not verify self-PR status or head drift. Reviewed.

Not explored to full depth (tool budget reached): "agent 1a": running the new test to confirm end-to-end reachability and green status (no pre-built core/target/classes ; I did not build in the shared worktree).**; "agent 6c": JVM-cost / bytecode-size measurement of readFieldName() after the guard was added (static javac + javap or PrintInlining ) — readFieldName() is off the h…; "agent 1c": static bytecode measurement of readFieldName() before/after (no JDK on PATH); "agent 1c": open the reflection ObjectReader field-name loop to confirm it handles JSONException from readFieldName(); "agent reverse-audit (round 2)": dynamic confirmation (compile + run JSON.parseObject("{\"a\\u00\"}") against a built target/classes ) of whether the ArrayIndexOutOfBoundsException is reac…, and 5 more.

— DeepSeek-V4.1-Flash-1017 via Qwen Code /review (v0.26.0)

@Support-itecz

Copy link
Copy Markdown
Contributor Author

Ran the items the review couldn't get to, against 6261e45 (the sha in the ledger, so no head drift):

  • FieldNameEscapeTruncationTest is green on the PR head (2 tests, 0 failures) and fails with ArrayIndexOutOfBoundsException on main.
  • readFieldName() bytecode goes from ~2746 to ~2778 bytes (+32 for the compare and throw). It's well past the 325-byte C2 inline threshold either way, so the guard doesn't change its inlining status, and the extra compare only runs on the \ path.
  • {"a\u00"} already produced JSONException: syntax error : 9 before this PR; the offset += 6 overshoot ends the loop and the trailing check catches it. The AIOOBE needs the \ to be the final byte. On main, {"\ and {"ab\ throw ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 (and Index 5 ... length 5) for both String and byte[] input; on this branch both throw invalid escape character EOI.
  • The exception is the same JSONException the value-string escape sites in this class already throw, so callers of readFieldName() (ObjectReader field-name loops included) don't see a new exception type.

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.

2 participants