Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public Date read(JsonReader in) throws IOException {
in.nextNull();
return null;
case NUMBER:
return new Date(in.nextInt() * 1000);
long seconds = in.nextLong();

@augmentcode augmentcode Bot Sep 21, 2026

Copy link
Copy Markdown

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

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

try {
return new Date(Math.multiplyExact(seconds, 1000L));
} catch (ArithmeticException e) {
throw new JsonParseException("Timestamp seconds out of range: " + seconds, e);
}
default:
throw new JsonParseException("Expected NUMBER but was " + peek);
}
Expand Down
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");
}
}
1 change: 1 addition & 0 deletions weixin-java-common/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<class name="me.chanjar.weixin.common.error.WxErrorTest"/>
<class name="me.chanjar.weixin.common.bean.WxMenuTest"/>
<class name="me.chanjar.weixin.common.util.crypto.WxCryptUtilTest"/>
<class name="me.chanjar.weixin.common.util.json.WxDateTypeAdapterTest"/>
<class name="me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateCheckerTest"/>
<class name="me.chanjar.weixin.common.session.SessionTest"/>
</classes>
Expand Down
Loading