diff options
author | Andy Pan <panjf2000@gmail.com> | 2021-03-12 19:40:46 +0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2021-03-17 19:48:52 +0000 |
commit | a5df88355cb8998856a5dbd3550b98e5de1f6009 (patch) | |
tree | 754da0398f9f9ed80f77092965dd446c8bb8a79a /src/time/time_test.go | |
parent | 5423f6023cff995f1077ac83e132705893a8af79 (diff) | |
download | go-a5df88355cb8998856a5dbd3550b98e5de1f6009.tar.gz go-a5df88355cb8998856a5dbd3550b98e5de1f6009.zip |
time: check int64 overflow in Time.addSec
Change-Id: Ibbed54239228e7ea31ef5978d427425899c3b943
Reviewed-on: https://go-review.googlesource.com/c/go/+/300890
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Baokun Lee <bk@golangcn.org>
Diffstat (limited to 'src/time/time_test.go')
-rw-r--r-- | src/time/time_test.go | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/time/time_test.go b/src/time/time_test.go index 8884731e1d..3a58bfe4e9 100644 --- a/src/time/time_test.go +++ b/src/time/time_test.go @@ -1481,7 +1481,7 @@ func TestTimeIsDST(t *testing.T) { tzFixed := FixedZone("FIXED_TIME", 12345) tests := [...]struct { - time Time + time Time want bool }{ 0: {Date(2009, 1, 1, 12, 0, 0, 0, UTC), false}, @@ -1501,3 +1501,28 @@ func TestTimeIsDST(t *testing.T) { } } } + +func TestTimeAddSecOverflow(t *testing.T) { + // Test it with positive delta. + var maxInt64 int64 = 1<<63 - 1 + timeExt := maxInt64 - UnixToInternal - 50 + notMonoTime := Unix(timeExt, 0) + for i := int64(0); i < 100; i++ { + sec := notMonoTime.Unix() + notMonoTime = notMonoTime.Add(Duration(i * 1e9)) + if newSec := notMonoTime.Unix(); newSec != sec+i && newSec+UnixToInternal != maxInt64 { + t.Fatalf("time ext: %d overflows with positive delta, overflow threshold: %d", newSec, maxInt64) + } + } + + // Test it with negative delta. + maxInt64 = -maxInt64 + notMonoTime = NotMonoNegativeTime + for i := int64(0); i > -100; i-- { + sec := notMonoTime.Unix() + notMonoTime = notMonoTime.Add(Duration(i * 1e9)) + if newSec := notMonoTime.Unix(); newSec != sec+i && newSec+UnixToInternal != maxInt64 { + t.Fatalf("time ext: %d overflows with positive delta, overflow threshold: %d", newSec, maxInt64) + } + } +} |