Skip to content

fix: bound readString0 whitespace skip at end of input - #7935

Open
Huang-404-Q wants to merge 1 commit into
alibaba:mainfrom
Huang-404-Q:fix/readstring0-eof-bounds
Open

Huang-404-Q wants to merge 1 commit into
alibaba:mainfrom
Huang-404-Q:fix/readstring0-eof-bounds

Conversation

@Huang-404-Q

Copy link
Copy Markdown

Problem

When a quoted token ends the input, readString0() skips the whitespace after the closing quote with an unbounded bytes[++offset], so it reads past the buffer and throws ArrayIndexOutOfBoundsException instead of the intended JSONException:

JSON.parseObject("\" 3 \"", Integer.class);
// java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5

The same happens through all three readers:

input reader exception
"\" abc\"" JSONReaderASCII Index 5 out of bounds for length 5
"\"中\"" as UTF-8 bytes JSONReaderUTF8 Index 6 out of bounds for length 6
"\"中\"" as String JSONReaderUTF16 Index 4 out of bounds for length 4

An ArrayIndexOutOfBoundsException escapes the parsing API, so callers that catch JSONException for malformed input no longer see a parse error — they see an unchecked runtime exception.

Fix

Bound the lookahead the same way the other read paths in this package already do, using the existing EOI sentinel (JSONReader.EOI, 0x1A, which is not in the SPACE bitmask so the skip loop terminates):

int ch = offset + 1 >= end ? EOI : bytes[++offset];

Three call sites in JSONReaderASCII, JSONReaderUTF8 (two lines each) and JSONReaderUTF16 (one line). The added bytecode is a single bounds compare on a path that already existed; it follows the pattern of the earlier readFieldName fixes rather than introducing a new one.

Tests

New JSONReaderReadString0BoundsTest covers the three readers plus a control case that quoted numbers still parse ("123", "1.5", with and without trailing whitespace).

On main:

Tests run: 4, Failures: 3
JSONReaderReadString0BoundsTest.testAsciiStringAtEnd
  expected: <com.alibaba.fastjson2.JSONException> but was: <java.lang.ArrayIndexOutOfBoundsException>

With the fix:

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS

-pl core validate is clean. Full -pl core test reports 8347 tests with 10 failures that also fail on unmodified main in this environment (missing CSV fixtures, a path containing non-ASCII characters), so they are unrelated to this change.

This is a different path from the earlier readFieldName bounds fixes (#7827, #7833): readString0 handles the token after a quoted value, not the field name.

中文说明

问题

当带引号的 token 位于输入末尾时,readString0() 在跳过闭合引号之后的空白时使用了无边界判定的 bytes[++offset],会越过缓冲区:

JSON.parseObject("\" 3 \"", Integer.class);
// java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5

三个 reader 都能复现(ASCII / UTF8 / UTF16)。ArrayIndexOutOfBoundsException 会直接逃出解析 API,调用方按 JSONException 捕获畸形输入时就不再是解析错误,而是未受检的运行时异常。

修复

沿用本包其它读取路径已有的写法,用现成的 EOI 哨兵(JSONReader.EOI,即 0x1A,不在 SPACE 位图里,因此跳过循环能正常结束)给前瞻加边界判断:

int ch = offset + 1 >= end ? EOI : bytes[++offset];

共三处:JSONReaderASCII、JSONReaderUTF8 各两行,JSONReaderUTF16 一行。新增字节码只是一次边界比较,沿用之前 readFieldName 系列修复的模式,没有引入新写法。

测试

新增 JSONReaderReadString0BoundsTest,覆盖三个 reader,并带一组对照用例确认带引号的数字仍能正常解析("123"、"1.5",含/不含尾随空白)。

在 main 上:Tests run: 4, Failures: 3,报 expected: <JSONException> but was: <ArrayIndexOutOfBoundsException>。
加上修复后:Tests run: 4, Failures: 0, Errors: 0, Skipped: 0,BUILD SUCCESS。

-pl core validate 通过。全量 -pl core test 共 8347 项,其中 10 项失败在未修改的 main 上同样失败(缺 CSV 夹具、路径含中文),与本改动无关。

与之前的 readFieldName 边界修复(#7827、#7833)不是同一处:readString0 处理的是带引号值之后的 token,不是字段名。

readString0() advanced past the closing quote with an unguarded
bytes[++offset] / chars[++offset] read. When the quoted token is the last
token of the input (optionally followed by whitespace), the lookahead ran
past the buffer and JSON.parseObject("\" 3 \"", Integer.class) threw
ArrayIndexOutOfBoundsException instead of JSONException.

Guard the read with the offset/end check already used by readFieldName,
and stop skipping whitespace at EOI.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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