aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2022-04-25 14:06:10 -0400
committerGopher Robot <gobot@golang.org>2022-04-26 02:28:58 +0000
commit17d7983b29ba633708a9d7b18f90ab5bc029502d (patch)
tree37a2799a2c7955a770fa5eb73435f031b6a863a3
parent09ada1af8f54584e46deb0d643713393a9d83b10 (diff)
downloadgo-17d7983b29ba633708a9d7b18f90ab5bc029502d.tar.gz
go-17d7983b29ba633708a9d7b18f90ab5bc029502d.zip
time: fix quickcheck test to avoid wraparounds
When we call time.Unix(s, ns), the internal representation is s + 62135596800, where 62135596800 is the number of seconds from Jan 1 1 to Jan 1 1970. If quickcheck generates numbers too close to 2^63, the addition can wraparound to make a very negative internal 64-bit value. Wraparounds are not guarded against, since they would not arise in any reasonable program, so just avoid testing near them. Fixes #52409. Change-Id: Id466c8a34a49055ab26f2687a6b2b657cb64bed6 Reviewed-on: https://go-review.googlesource.com/c/go/+/402177 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
-rw-r--r--src/time/time_test.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/time/time_test.go b/src/time/time_test.go
index 1701401ab4..695d48b1b5 100644
--- a/src/time/time_test.go
+++ b/src/time/time_test.go
@@ -281,6 +281,8 @@ func TestTruncateRound(t *testing.T) {
b1e9.SetInt64(1e9)
testOne := func(ti, tns, di int64) bool {
+ t.Helper()
+
t0 := Unix(ti, int64(tns)).UTC()
d := Duration(di)
if d < 0 {
@@ -367,6 +369,13 @@ func TestTruncateRound(t *testing.T) {
for i := 0; i < int(b); i++ {
d *= 5
}
+
+ // Make room for unix ↔ internal conversion.
+ // We don't care about behavior too close to ± 2^63 Unix seconds.
+ // It is full of wraparounds but will never happen in a reasonable program.
+ // (Or maybe not? See go.dev/issue/20678. In any event, they're not handled today.)
+ ti >>= 1
+
return testOne(ti, int64(tns), int64(d))
}
quick.Check(f1, cfg)
@@ -377,6 +386,7 @@ func TestTruncateRound(t *testing.T) {
if d < 0 {
d = -d
}
+ ti >>= 1 // see comment in f1
return testOne(ti, int64(tns), int64(d))
}
quick.Check(f2, cfg)
@@ -399,6 +409,7 @@ func TestTruncateRound(t *testing.T) {
// full generality
f4 := func(ti int64, tns int32, di int64) bool {
+ ti >>= 1 // see comment in f1
return testOne(ti, int64(tns), di)
}
quick.Check(f4, cfg)