fix(common): 修复日期时间戳解析溢出 - #4129
binarywang wants to merge 1 commit into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
🤖 Augment PR Summary摘要:本 PR 修复
🤖 Was this summary useful? React with 👍 or 👎 |
| return null; | ||
| case NUMBER: | ||
| return new Date(in.nextInt() * 1000); | ||
| long seconds = in.nextLong(); |
There was a problem hiding this comment.
JsonReader.nextLong() 对部分超出 long 范围的数字(如 9223372036854775808)会经 double 回退后饱和为 Long.MAX_VALUE,所以这里最终报出的时间戳是 9223372036854775807,并非原始输入;更大的数值则会直接抛出 NumberFormatException。这使溢出场景的异常类型和“带时间戳上下文”的错误信息不一致,可能误导上游的错误处理和排查。
Severity: low
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
变更范围聚焦且实现与测试覆盖一致,溢出风险已通过 Math.multiplyExact 与回归测试验证并纳入 TestNG suite。
Review effort: Lite
Findings: None
What changed in this PR
该 PR 修复 weixin-java-common 中 WxDateTypeAdapter 解析微信返回“秒级时间戳”时的 2038+ 与乘法溢出问题,避免因 int/long 溢出导致日期解析错误,并补齐回归测试以确保构建中实际执行。
Changes:
- 将时间戳读取从
JsonReader.nextInt()调整为JsonReader.nextLong(),并用Math.multiplyExact(seconds, 1000L)将秒转毫秒,显式捕获溢出。 - 溢出时抛出包含原始 seconds 值上下文的
JsonParseException(保留 cause)。 - 新增
WxDateTypeAdapterTest并加入 TestNG suite,确保 Maven surefire 会运行该回归测试。
| File | Description |
|---|---|
| weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxDateTypeAdapter.java | 使用 nextLong + multiplyExact 修复秒级时间戳解析与乘法溢出问题,并在溢出时抛出带上下文的 JsonParseException。 |
| weixin-java-common/src/test/java/me/chanjar/weixin/common/util/json/WxDateTypeAdapterTest.java | 新增覆盖 2038+ 时间戳解析、正/负方向溢出拒绝、写出秒级时间戳的回归测试。 |
| weixin-java-common/src/test/resources/testng.xml | 将 WxDateTypeAdapterTest 加入 suite,确保默认 Maven 测试执行覆盖该用例。 |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
变更说明
重新提交并完善已关闭的 #4125:
JsonReader.nextLong()读取微信返回的秒级时间戳,支持 2038 年后的时间。Math.multiplyExact()将秒转换为毫秒,防止long乘法静默溢出。JsonParseException。WxDateTypeAdapterTest加入 TestNG suite,确保 Maven 构建实际执行回归测试。验证
mvn -pl weixin-java-common -am -Dmaven.test.skip=false test