Repository navigation
fix: bound readString0 whitespace skip at end of input - #7935
Open
Huang-404-Q wants to merge 1 commit into
Open
Huang-404-Q wants to merge 1 commit into
Huang-404-Q wants to merge 1 commit into
Conversation
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.
|
|
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.
Problem
When a quoted token ends the input,
readString0()skips the whitespace after the closing quote with an unboundedbytes[++offset], so it reads past the buffer and throwsArrayIndexOutOfBoundsExceptioninstead of the intendedJSONException:The same happens through all three readers:
"\" abc\""JSONReaderASCIIIndex 5 out of bounds for length 5"\"中\""as UTF-8 bytesJSONReaderUTF8Index 6 out of bounds for length 6"\"中\""asStringJSONReaderUTF16Index 4 out of bounds for length 4An
ArrayIndexOutOfBoundsExceptionescapes the parsing API, so callers that catchJSONExceptionfor 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
EOIsentinel (JSONReader.EOI,0x1A, which is not in theSPACEbitmask so the skip loop terminates):Three call sites in
JSONReaderASCII,JSONReaderUTF8(two lines each) andJSONReaderUTF16(one line). The added bytecode is a single bounds compare on a path that already existed; it follows the pattern of the earlierreadFieldNamefixes rather than introducing a new one.Tests
New
JSONReaderReadString0BoundsTestcovers the three readers plus a control case that quoted numbers still parse ("123","1.5", with and without trailing whitespace).On
main:With the fix:
-pl core validateis clean. Full-pl core testreports 8347 tests with 10 failures that also fail on unmodifiedmainin 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
readFieldNamebounds fixes (#7827, #7833):readString0handles the token after a quoted value, not the field name.中文说明
问题
当带引号的 token 位于输入末尾时,
readString0()在跳过闭合引号之后的空白时使用了无边界判定的bytes[++offset],会越过缓冲区:三个 reader 都能复现(ASCII / UTF8 / UTF16)。
ArrayIndexOutOfBoundsException会直接逃出解析 API,调用方按JSONException捕获畸形输入时就不再是解析错误,而是未受检的运行时异常。修复
沿用本包其它读取路径已有的写法,用现成的
EOI哨兵(JSONReader.EOI,即0x1A,不在SPACE位图里,因此跳过循环能正常结束)给前瞻加边界判断:共三处:
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,不是字段名。