Repository navigation
fix: keep DateUtils.parseMillis reads inside the off/len window - #7929
Open
Support-itecz wants to merge 1 commit into
Open
Support-itecz wants to merge 1 commit into
Support-itecz wants to merge 1 commit into
Conversation
parseMillis(byte[]) and parseMillis(char[]) probe for "0000-00-00" with a 10-element Unsafe load at off without checking len, so a short value at the end of an array is read past the array end, and "0000-00-" followed by "00" outside the window parses as 1970-01-01. The quote and trailing 'Z' checks index chars[len - 1] instead of chars[off + len - 1], so a quoted date at off > 0 is rejected. Gate the probe on len >= 10, index the last element from off, and throw DateTimeParseException from the byte[] overload for unparseable input, as the char[] overload already does, instead of an NPE.
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.
What this PR does / why we need it?
DateUtils.parseMillis(byte[], off, len, ...)andparseMillis(char[], off, len, ...)read outside the[off, off + len)window they are given:parseLocalDateTimereturns null, the"0000-00-00"fallback does a 10-elementUnsafeload (getLongLE/getShortLE/getIntLE) atoffwithout checkinglen. For a short value near the end of the array this reads past the array end, which is the same class as theFnv.hashCode64fix in fix(core): Fnv.hashCode64 fast path must not read past the end of the input array #7874. It also lets the window0000-00-borrow00from the caller's buffer:parseMillis("0000-00-00".getBytes(), 0, 8)returns 1970-01-01, while an exact-size copy of the same 8 bytes is rejected.'Z'check indexchars[len - 1]instead ofchars[off + len - 1], soparseMillis("xx\"2024-01-02\"".getBytes(), 2, 12)fails withStringIndexOutOfBoundsExceptioneven though the window holds a valid quoted date.These overloads take windows of a larger buffer from
CSVReaderUTF8/CSVReaderUTF16(Datecolumns) and the ArrowArrowByteArrayConsumer, so the CSV or Arrow input decides what lands at the end of the window.Summary of your change
len >= 10in both overloads, so the load stays inside the window.off + len - 1for the quote and'Z'checks.parseMillis(byte[])now throwsDateTimeParseExceptionfor unparseable input, as thechar[]overload already does, instead of an NPE fromZonedDateTime.ofLocal(null).DateUtilsTest.parseMillisStaysInWindow. It fails onmainand passes with the fix. Valid input is unchanged, including0000-00-00at a non-zero offset.Verified with
./mvnw -pl core test(8344 tests, 0 failures), the same suite with-Dfastjson2.creator=reflect, and./mvnw validate. Nothing here is on the JSON parse hot path; the extra check runs only afterparseLocalDateTimehas already failed.This fix was found and prepared with AI assistance and reviewed by me before submitting.
Please indicate you've done the following:
中文
这个 PR 做了什么 / 为什么需要?
DateUtils.parseMillis(byte[], off, len, ...)和parseMillis(char[], off, len, ...)会读取传入窗口[off, off + len)之外的数据:parseLocalDateTime返回 null 时,"0000-00-00"兜底判断在off处通过Unsafe读取 10 个元素(getLongLE/getShortLE/getIntLE),没有检查len。值较短且靠近数组末尾时会越过数组末尾读取,与 fix(core): Fnv.hashCode64 fast path must not read past the end of the input array #7874 中Fnv.hashCode64的问题属于同一类。此外窗口0000-00-会借用调用方缓冲区中后面的00:parseMillis("0000-00-00".getBytes(), 0, 8)返回 1970-01-01,而同样 8 个字节的独立拷贝会被拒绝。'Z'判断使用chars[len - 1]而不是chars[off + len - 1],因此parseMillis("xx\"2024-01-02\"".getBytes(), 2, 12)会抛出StringIndexOutOfBoundsException,尽管窗口内是合法的带引号日期。CSVReaderUTF8/CSVReaderUTF16(Date列)以及 Arrow 的ArrowByteArrayConsumer都以大缓冲区中的窗口调用这两个方法,窗口末尾是什么由 CSV 或 Arrow 输入决定。改动概要
len >= 10条件,读取不再越出窗口。'Z'判断改为off + len - 1。parseMillis(byte[])对无法解析的输入抛出DateTimeParseException,与char[]重载一致,不再由ZonedDateTime.ofLocal(null)抛出 NPE。DateUtilsTest.parseMillisStaysInWindow,在main上失败,修复后通过。合法输入行为不变,包括非零偏移处的0000-00-00。已验证
./mvnw -pl core test(8344 个测试,0 失败)、-Dfastjson2.creator=reflect模式以及./mvnw validate。改动不在 JSON 解析热路径上,新增判断只在parseLocalDateTime已失败后执行。