From 6bddcd7f644e8234468ef78282466448350ae0cf Mon Sep 17 00:00:00 2001 From: Josh Humphries <2035234+jhump@users.noreply.github.com> Date: Thu, 16 Jan 2025 09:13:40 -0500 Subject: [PATCH] avoid overflow in various timeouts --- cmd/grpcurl/grpcurl.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/grpcurl/grpcurl.go b/cmd/grpcurl/grpcurl.go index 5604ac48..b7942886 100644 --- a/cmd/grpcurl/grpcurl.go +++ b/cmd/grpcurl/grpcurl.go @@ -8,6 +8,7 @@ import ( "flag" "fmt" "io" + "math" "os" "path/filepath" "strconv" @@ -457,7 +458,7 @@ func main() { ctx := context.Background() if *maxTime > 0 { - timeout := time.Duration(*maxTime * float64(time.Second)) + timeout := floatSecondsToDuration(*maxTime) var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, timeout) defer cancel() @@ -468,13 +469,13 @@ func main() { defer dialTiming.Done() dialTime := 10 * time.Second if *connectTimeout > 0 { - dialTime = time.Duration(*connectTimeout * float64(time.Second)) + dialTime = floatSecondsToDuration(*connectTimeout) } ctx, cancel := context.WithTimeout(ctx, dialTime) defer cancel() var opts []grpc.DialOption if *keepaliveTime > 0 { - timeout := time.Duration(*keepaliveTime * float64(time.Second)) + timeout := floatSecondsToDuration(*keepaliveTime) opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{ Time: timeout, Timeout: timeout, @@ -971,3 +972,12 @@ func (f *optionalBoolFlag) Set(s string) error { func (f *optionalBoolFlag) IsBoolFlag() bool { return true } + +func floatSecondsToDuration(seconds float64) time.Duration { + durationFloat := seconds * float64(time.Second) + if durationFloat > math.MaxInt64 { + // Avoid overflow + return math.MaxInt64 + } + return time.Duration(durationFloat) +}