Skip to content

Commit d00c281

Browse files
authored
avoid overflow in various timeouts (#505)
1 parent 9e3e083 commit d00c281

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

cmd/grpcurl/grpcurl.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"flag"
99
"fmt"
1010
"io"
11+
"math"
1112
"os"
1213
"path/filepath"
1314
"strconv"
@@ -457,7 +458,7 @@ func main() {
457458

458459
ctx := context.Background()
459460
if *maxTime > 0 {
460-
timeout := time.Duration(*maxTime * float64(time.Second))
461+
timeout := floatSecondsToDuration(*maxTime)
461462
var cancel context.CancelFunc
462463
ctx, cancel = context.WithTimeout(ctx, timeout)
463464
defer cancel()
@@ -468,13 +469,13 @@ func main() {
468469
defer dialTiming.Done()
469470
dialTime := 10 * time.Second
470471
if *connectTimeout > 0 {
471-
dialTime = time.Duration(*connectTimeout * float64(time.Second))
472+
dialTime = floatSecondsToDuration(*connectTimeout)
472473
}
473474
ctx, cancel := context.WithTimeout(ctx, dialTime)
474475
defer cancel()
475476
var opts []grpc.DialOption
476477
if *keepaliveTime > 0 {
477-
timeout := time.Duration(*keepaliveTime * float64(time.Second))
478+
timeout := floatSecondsToDuration(*keepaliveTime)
478479
opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
479480
Time: timeout,
480481
Timeout: timeout,
@@ -971,3 +972,12 @@ func (f *optionalBoolFlag) Set(s string) error {
971972
func (f *optionalBoolFlag) IsBoolFlag() bool {
972973
return true
973974
}
975+
976+
func floatSecondsToDuration(seconds float64) time.Duration {
977+
durationFloat := seconds * float64(time.Second)
978+
if durationFloat > math.MaxInt64 {
979+
// Avoid overflow
980+
return math.MaxInt64
981+
}
982+
return time.Duration(durationFloat)
983+
}

0 commit comments

Comments
 (0)