-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix(common): 修复日期时间戳解析溢出 #4129
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
Open
binarywang
wants to merge
1
commit into
develop
Choose a base branch
from
fix/wx-date-type-adapter-overflow
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(common): 修复日期时间戳解析溢出 #4129
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
...n-java-common/src/test/java/me/chanjar/weixin/common/util/json/WxDateTypeAdapterTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package me.chanjar.weixin.common.util.json; | ||
|
|
||
| import com.google.gson.JsonParseException; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Date; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** | ||
| * Tests for {@link WxDateTypeAdapter}. | ||
| */ | ||
| public class WxDateTypeAdapterTest { | ||
| private final WxDateTypeAdapter adapter = new WxDateTypeAdapter(); | ||
|
|
||
| @Test | ||
| public void testReadTimestampAfter2038() throws IOException { | ||
| Date date = this.adapter.fromJson("4102444800"); | ||
|
|
||
| assertThat(date).isEqualTo(new Date(4102444800000L)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadRejectsPositiveMillisecondOverflow() { | ||
| long overflowingSeconds = Long.MAX_VALUE / 1000 + 1; | ||
|
|
||
| assertThatThrownBy(() -> this.adapter.fromJson(Long.toString(overflowingSeconds))) | ||
| .isInstanceOf(JsonParseException.class) | ||
| .hasMessageContaining("out of range"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadRejectsNegativeMillisecondOverflow() { | ||
| long overflowingSeconds = Long.MIN_VALUE / 1000 - 1; | ||
|
|
||
| assertThatThrownBy(() -> this.adapter.fromJson(Long.toString(overflowingSeconds))) | ||
| .isInstanceOf(JsonParseException.class) | ||
| .hasMessageContaining("out of range"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWriteUsesSeconds() throws IOException { | ||
| assertThat(this.adapter.toJson(new Date(4102444800123L))).isEqualTo("4102444800"); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
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.