Skip to content

fix: handle truncated escape in JSONReaderUTF16 - #7791

Merged
wenshao merged 4 commits into
alibaba:mainfrom
annoymous3:fix/truncated-backslash-escape
Oct 17, 2026
Merged

wenshao merged 4 commits into
alibaba:mainfrom
annoymous3:fix/truncated-backslash-escape

Conversation

@annoymous3

Copy link
Copy Markdown

这个 PR 做了什么 / 为什么需要这个修改?

当 JSON 字符串以被截断的反斜杠转义结尾时,JSONReaderUTF16.readString() 会读取字符缓冲区末尾之外。

例如,在 JDK 8 上执行 JSON.parse("\"\\") 会抛出 ArrayIndexOutOfBoundsException,而不是 JSONException。由于 ArrayIndexOutOfBoundsException 不是 JSONException 的子类,调用方无法通过 catch (JSONException) 处理该非法输入。

本次修改在读取反斜杠后的字符之前检查是否已经到达输入末尾,并抛出 JSONException,使 UTF16 reader 的行为与 UTF8 reader 及其他非法 JSON 输入保持一致。

修改摘要

  • 在 JSONReaderUTF16.readString() 的反斜杠处理分支增加输入结束检查。
  • 增加根字符串、对象属性值和数组元素中截断转义的回归测试。
  • 覆盖 String、char[] 和 UTF8 字节数组解析入口。
  • 普通无转义字符串的处理路径保持不变。

已执行以下验证:

mvn -pl core -DskipTests compile
mvn -pl core -Dtest=JSONReaderUTF16Test surefire:test

上述命令均执行成功。

#7790

@CLAassistant

CLAassistant commented Aug 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@wenshao
wenshao dismissed their stale review October 17, 2026 08:24

Dismissing stale review: R1-1 (EOI guard added to only one of the structurally identical sites) is addressed by 27aa75c, which routes all JSONReaderUTF16 escape sites through the bounded char2(chars, offset, end), plus the sibling JSONReaderUTF8; R1-2/R1-3 are test notes. CI green on head. Agent-verified against the head tree.

# Conflicts:
#	core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
#	core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
@wenshao
wenshao merged commit cf895a8 into alibaba:main Oct 17, 2026
1 check was pending
@wenshao wenshao mentioned this pull request Oct 17, 2026
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.

4 participants