Skip to content

fix: keep DateUtils.parseMillis reads inside the off/len window - #7929

Open
Support-itecz wants to merge 1 commit into
alibaba:mainfrom
Support-itecz:dateutils-parsemillis-window
Open

Support-itecz wants to merge 1 commit into
alibaba:mainfrom
Support-itecz:dateutils-parsemillis-window

Conversation

@Support-itecz

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

DateUtils.parseMillis(byte[], off, len, ...) and parseMillis(char[], off, len, ...) read outside the [off, off + len) window they are given:

  • When parseLocalDateTime returns null, the "0000-00-00" fallback does a 10-element Unsafe load (getLongLE/getShortLE/getIntLE) at off without checking len. For a short value near the end of the array this reads past the array end, which is the same class as the Fnv.hashCode64 fix in fix(core): Fnv.hashCode64 fast path must not read past the end of the input array #7874. It also lets the window 0000-00- borrow 00 from 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.
  • The quote check and the trailing 'Z' check index chars[len - 1] instead of chars[off + len - 1], so parseMillis("xx\"2024-01-02\"".getBytes(), 2, 12) fails with StringIndexOutOfBoundsException even though the window holds a valid quoted date.

These overloads take windows of a larger buffer from CSVReaderUTF8 / CSVReaderUTF16 (Date columns) and the Arrow ArrowByteArrayConsumer, so the CSV or Arrow input decides what lands at the end of the window.

Summary of your change

  • Gate the zero-date probe on len >= 10 in both overloads, so the load stays inside the window.
  • Index the last element as off + len - 1 for the quote and 'Z' checks.
  • parseMillis(byte[]) now throws DateTimeParseException for unparseable input, as the char[] overload already does, instead of an NPE from ZonedDateTime.ofLocal(null).
  • Added DateUtilsTest.parseMillisStaysInWindow. It fails on main and passes with the fix. Valid input is unchanged, including 0000-00-00 at 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 after parseLocalDateTime has already failed.

This fix was found and prepared with AI assistance and reviewed by me before submitting.

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.
中文

这个 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 已失败后执行。

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

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.

1 participant