1
0
mirror of synced 2026-10-08 07:46:22 +08:00

🎨 修复日期时间戳解析溢出问题

This commit is contained in:
Binary Wang
2026-09-26 15:14:55 +08:00
committed by GitHub
parent a91a0140fc
commit 58af116149
3 changed files with 54 additions and 1 deletions

View File

@@ -35,7 +35,12 @@ public class WxDateTypeAdapter extends TypeAdapter<Date> {
in.nextNull();
return null;
case NUMBER:
return new Date(in.nextInt() * 1000);
long seconds = in.nextLong();
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);
}

View File

@@ -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");
}
}

View File

@@ -8,6 +8,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>