Skip to content

Commit 486902c

Browse files
committed
Fix possible int overflow
1 parent 8895197 commit 486902c

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

pkg/fetching/download.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ type Download struct {
7272
firstByteIndex int64
7373
lastByteIndex int64
7474

75-
client *http.Client
76-
request *http.Request
77-
cancelRequest context.CancelFunc
78-
cooldownTime time.Time
79-
cooldownStacks int
75+
client *http.Client
76+
request *http.Request
77+
cancelRequest context.CancelFunc
78+
cooldownTime time.Time
79+
cooldownIndex int
8080

8181
response *http.Response
8282
responseReader io.Reader
@@ -261,7 +261,7 @@ func (dl *Download) cleanUp() {
261261

262262
func (dl *Download) waitCooldown() {
263263
now := time.Now()
264-
if dl.cooldownStacks > 0 && dl.cooldownTime.After(now) {
264+
if dl.cooldownIndex > 0 && dl.cooldownTime.After(now) {
265265
select {
266266
case <-time.NewTimer(dl.cooldownTime.Sub(now)).C:
267267
case <-dl.ctx.Done():
@@ -272,8 +272,7 @@ func (dl *Download) waitCooldown() {
272272

273273
func (dl *Download) inscribeCooldown() {
274274
cooldownIntervalOptions := []time.Duration{1, 1, 2, 3, 5, 8, 13}
275-
cooldownOptionIndex := intMin(dl.cooldownStacks, len(cooldownIntervalOptions)-1)
276-
cooldownDuration := time.Second * cooldownIntervalOptions[cooldownOptionIndex]
275+
cooldownDuration := time.Second * cooldownIntervalOptions[dl.cooldownIndex]
277276

278277
p := make([]byte, 1)
279278
_, err := rand.Read(p)
@@ -285,11 +284,11 @@ func (dl *Download) inscribeCooldown() {
285284
}
286285

287286
dl.cooldownTime = time.Now().Add(cooldownDuration)
288-
dl.cooldownStacks++
287+
dl.cooldownIndex = intMin(dl.cooldownIndex+1, len(cooldownIntervalOptions)-1)
289288
}
290289

291290
func (dl *Download) resetCooldown() {
292-
dl.cooldownStacks = 0
291+
dl.cooldownIndex = 0
293292
}
294293

295294
func intMin(a, b int) int {

0 commit comments

Comments
 (0)