-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix(common): 修复日期解析 int 溢出问题 #4125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package me.chanjar.weixin.common.util.json; | ||
|
|
||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.StringReader; | ||
| import java.io.StringWriter; | ||
| import java.util.Date; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * WxDateTypeAdapter 的单元测试. | ||
| * | ||
| * @author liyong | ||
| */ | ||
| public class WxDateTypeAdapterTest { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
AGENTS.md reference: AGENTS.md:L63-L65 Useful? React with 👍 / 👎. |
||
|
|
||
| private final WxDateTypeAdapter adapter = new WxDateTypeAdapter(); | ||
|
|
||
| private Date read(String json) throws IOException { | ||
| JsonReader reader = new JsonReader(new StringReader(json)); | ||
| reader.setLenient(true); | ||
| return adapter.read(reader); | ||
| } | ||
|
|
||
| private String write(Date date) throws IOException { | ||
| StringWriter out = new StringWriter(); | ||
| JsonWriter writer = new JsonWriter(out); | ||
| writer.setLenient(true); | ||
| adapter.write(writer, date); | ||
| return out.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * 秒级时间戳转成毫秒时如果按 int 运算会溢出,导致解析出的时间错误(甚至早于 1970 年)。 | ||
| */ | ||
| @Test | ||
| public void testReadCurrentTimestamp() throws IOException { | ||
| long seconds = 1481013459L; | ||
| Date date = read(String.valueOf(seconds)); | ||
| assertThat(date).isNotNull(); | ||
| assertThat(date.getTime()).isEqualTo(seconds * 1000L); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadTimestampAfterYear2038() throws IOException { | ||
| long seconds = 4102444800L; | ||
| Date date = read(String.valueOf(seconds)); | ||
| assertThat(date).isNotNull(); | ||
| assertThat(date.getTime()).isEqualTo(seconds * 1000L); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadNull() throws IOException { | ||
| assertThat(read("null")).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWrite() throws IOException { | ||
| long seconds = 1481013459L; | ||
| assertThat(write(new Date(seconds * 1000L))).isEqualTo(String.valueOf(seconds)); | ||
| assertThat(write(null)).isEqualTo("null"); | ||
| } | ||
|
|
||
| /** | ||
| * 序列化与反序列化应当可以互相还原。 | ||
| */ | ||
| @Test | ||
| public void testWriteThenRead() throws IOException { | ||
| Date now = new Date(1600000000L * 1000L); | ||
| assertThat(read(write(now))).isEqualTo(now); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当输入是
nextLong()可接受、但绝对值超过毫秒换算范围的秒值时(例如9223372036854776),这里的乘法会发生long溢出并静默生成完全错误的Date;旧实现会因超出int范围而拒绝该输入。请在乘法前校验边界或使用Math.multiplyExact并将溢出转换为解析异常,避免异常响应被当作有效日期。AGENTS.md reference: AGENTS.md:L47-L48
Useful? React with 👍 / 👎.