From 248cf194cdb8dfd4eb23753337a7f2fab14cf9a4 Mon Sep 17 00:00:00 2001
From: Jack Doan <jackdoan@rivian.com>
Date: Tue, 13 Aug 2024 06:25:18 -0700
Subject: [PATCH] fix integer wraparound in the calculation of handshake
 timeouts on 32-bit targets (#1185)

Fixes: #1169
---
 handshake_manager.go | 8 ++++----
 main.go              | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

--- a/handshake_manager.go
+++ b/handshake_manager.go
@@ -35,7 +35,7 @@
 
 type HandshakeConfig struct {
 	tryInterval   time.Duration
-	retries       int
+	retries       int64
 	triggerBuffer int
 	useRelays     bool
 
@@ -69,7 +69,7 @@
 
 	startTime   time.Time       // Time that we first started trying with this handshake
 	ready       bool            // Is the handshake ready
-	counter     int             // How many attempts have we made so far
+	counter     int64             // How many attempts have we made so far
 	lastRemotes []*udp.Addr     // Remotes that we sent to during the previous attempt
 	packetStore []*cachedPacket // A set of packets to be transmitted once the handshake completes
 
@@ -656,6 +656,6 @@
 	return index, nil
 }
 
-func hsTimeout(tries int, interval time.Duration) time.Duration {
-	return time.Duration(tries / 2 * ((2 * int(interval)) + (tries-1)*int(interval)))
+func hsTimeout(tries int64, interval time.Duration) time.Duration {
+	return time.Duration(tries / 2 * ((2 * int64(interval)) + (tries-1)*int64(interval)))
 }
--- a/main.go
+++ b/main.go
@@ -201,7 +201,7 @@
 
 	handshakeConfig := HandshakeConfig{
 		tryInterval:   c.GetDuration("handshakes.try_interval", DefaultHandshakeTryInterval),
-		retries:       c.GetInt("handshakes.retries", DefaultHandshakeRetries),
+		retries:       int64(c.GetInt("handshakes.retries", DefaultHandshakeRetries)),
 		triggerBuffer: c.GetInt("handshakes.trigger_buffer", DefaultHandshakeTriggerBuffer),
 		useRelays:     useRelays,
 
