From aa5069e16ed410a9547dc86145cbe940ed12d6cf Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Thu, 29 Aug 2024 14:10:14 +0300 Subject: [PATCH] Remove go type limits constants They can be retrieved directly from the std math package. Slightly optimize the Fib functions and improve comments. --- analyzers/analyzers.go | 3 ++- utils/consts.go | 9 --------- utils/coreutils.go | 15 +++++++++++---- utils/coreutils_test.go | 9 +++++++-- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/analyzers/analyzers.go b/analyzers/analyzers.go index 63b4e4bb1..2130277ae 100644 --- a/analyzers/analyzers.go +++ b/analyzers/analyzers.go @@ -21,6 +21,7 @@ package analyzers import ( "encoding/json" "fmt" + "math" "math/rand" "os" "path" @@ -154,7 +155,7 @@ func (aS *AnalyzerS) V1StringQuery(ctx *context.Context, args *QueryArgs, reply } else { q = bleve.NewQueryStringQuery(args.HeaderFilters) } - s := bleve.NewSearchRequestOptions(q, utils.AbsoluteMaxInt, 0, false) + s := bleve.NewSearchRequestOptions(q, math.MaxInt, 0, false) s.Fields = []string{utils.Meta} // return all fields searchResults, err := aS.db.SearchInContext(ctx, s) if err != nil { diff --git a/utils/consts.go b/utils/consts.go index a8d33abfa..ffbe9da1d 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -2794,15 +2794,6 @@ const ( ToNearestTowardZero = "*toNearestTowardZero" ) -// 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 285889a80..f7ad7b2f8 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -429,8 +429,11 @@ func copyFile(rc io.ReadCloser, path string, fm os.FileMode) (err error) { func Fib() func() int { a, b := 0, 1 return func() int { - if b > 0 { - a, b = b, a+b // only increment Fibonacci numbers while b doesn't overflow + a, b = b, a+b + + // Prevent int overflow by keeping b as the maximum valid Fibonacci number. + if b < a { + b = a } return a } @@ -442,11 +445,15 @@ func FibDuration(durationUnit, maxDuration time.Duration) func() time.Duration { fib := Fib() return func() time.Duration { 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 + + // Handle potential overflow when multiplying by durationUnit. + if fibNrAsDuration > (math.MaxInt / durationUnit) { + fibNrAsDuration = math.MaxInt } else { fibNrAsDuration *= durationUnit } + + // Cap the duration to maxDuration if specified. if maxDuration > 0 && maxDuration < fibNrAsDuration { return maxDuration } diff --git a/utils/coreutils_test.go b/utils/coreutils_test.go index 082406e25..5f7778659 100644 --- a/utils/coreutils_test.go +++ b/utils/coreutils_test.go @@ -20,6 +20,7 @@ package utils import ( "errors" "fmt" + "math" "reflect" "sync" "testing" @@ -1585,10 +1586,14 @@ func TestCoreUtilsFibDurationSeqNrOverflow(t *testing.T) { fib() } for i := 0; i < 100; i++ { - if rcv := fib(); rcv != AbsoluteMaxDuration { - t.Errorf("expected: <%+v>, \nreceived: <%+v>", AbsoluteMaxDuration, rcv) + if rcv := fib(); rcv != math.MaxInt { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", math.MaxInt, rcv) } } + fib = FibDuration(time.Second, 6) + if rcv := fib(); rcv != 6 { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", math.MaxInt, rcv) + } } func TestUnzip(t *testing.T) {