From 7003a2457da6a6e3ad4c316e19f0951fc682a67f Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Thu, 28 Jul 2022 17:00:26 +0300 Subject: [PATCH] Fix issue where fib function overflows --- utils/consts.go | 9 +++++++++ utils/coreutils.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/utils/consts.go b/utils/consts.go index ba4e54c8c..9585a2436 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -2784,6 +2784,15 @@ const ( HSuffix = "h" ) +// Go type limits +const ( + AbsoluteMaxUint = ^uint(0) + AbsoluteMinUint = 0 + AbsoluteMaxInt = int(AbsoluteMaxUint >> 1) + AbsoluteMinInt = -AbsoluteMaxInt - 1 + AbsoluteMaxDuration = time.Duration(AbsoluteMaxInt) +) + func buildCacheInstRevPrefixes() { CachePrefixToInstance = make(map[string]string) for k, v := range CacheInstanceToPrefix { diff --git a/utils/coreutils.go b/utils/coreutils.go index db8822a88..2197ece29 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -483,7 +483,9 @@ func copyFile(rc io.ReadCloser, path string, fm os.FileMode) (err error) { func Fib() func() int { a, b := 0, 1 return func() int { - a, b = b, a+b + if b > 0 { + a, b = b, a+b + } return a } } @@ -493,7 +495,12 @@ func Fib() func() int { func FibDuration(durationUnit, maxDuration time.Duration) func() time.Duration { fib := Fib() return func() time.Duration { - fibNrAsDuration := time.Duration(fib()) * durationUnit + fibNrAsDuration := time.Duration(fib()) + if fibNrAsDuration > (AbsoluteMaxDuration / durationUnit) { // check if the current fibonacci nr. in the sequence would exceed the absolute maximum duration if multiplied by the duration unit value + fibNrAsDuration = AbsoluteMaxDuration + } else { + fibNrAsDuration *= durationUnit + } if maxDuration > 0 && maxDuration < fibNrAsDuration { return maxDuration }