From b78b427be5e4c8a51a2b01b39c1ce6c4f39a93dc Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Wed, 2 Dec 2020 12:19:13 -0500 Subject: runtime, time: strictly enforce when, period constraints timer.when must always be positive. addtimer and modtimer already check that it is non-negative; we expand it to include zero. Also upgrade from pinning bad values to throwing, as these values shouldn't be possible to pass (except as below). timeSleep may overflow timer.nextwhen. This would previously have been pinned by resetForSleep, now we fix it manually. runOneTimer may overflow timer.when when adding timer.period. Detect this and pin to maxWhen. addtimer is now too strict to allow TestOverflowRuntimeTimer to test an overflowed timer. Such a timer should not be possible; to help guard against accidental inclusion siftup / siftdown will check timers as it goes. This has been replaced with tests for period and sleep overflows. Change-Id: I17f9739e27ebcb20d87945c635050316fb8e9226 Reviewed-on: https://go-review.googlesource.com/c/go/+/274853 Trust: Michael Pratt Reviewed-by: Michael Knyszek Reviewed-by: Ian Lance Taylor --- src/time/sleep.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/time/sleep.go') diff --git a/src/time/sleep.go b/src/time/sleep.go index 22ffd68282..90d8a18a68 100644 --- a/src/time/sleep.go +++ b/src/time/sleep.go @@ -31,6 +31,8 @@ func when(d Duration) int64 { } t := runtimeNano() + int64(d) if t < 0 { + // N.B. runtimeNano() and d are always positive, so addition + // (including overflow) will never result in t == 0. t = 1<<63 - 1 // math.MaxInt64 } return t -- cgit v1.3