Fix int overflow on x32 processors causing panic (#3810)

During the roll, "duration" is converted to int, causing overflow on x32 processors. The value may be very small or negative, causing a panic in rand.Intn.
This commit is contained in:
Kuzmin Vladimir 2024-09-15 06:47:16 +03:00 committed by GitHub
parent 781aaee21f
commit d7c5a0fc5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions

View File

@ -156,7 +156,7 @@ func (h *HealthPing) doCheck(tags []string, duration time.Duration, rounds int)
for i := 0; i < rounds; i++ { for i := 0; i < rounds; i++ {
delay := time.Duration(0) delay := time.Duration(0)
if duration > 0 { if duration > 0 {
delay = time.Duration(dice.Roll(int(duration))) delay = time.Duration(dice.RollInt63n(int64(duration)))
} }
time.AfterFunc(delay, func() { time.AfterFunc(delay, func() {
errors.LogDebug(h.ctx, "checking ", handler) errors.LogDebug(h.ctx, "checking ", handler)

View File

@ -14,6 +14,14 @@ func Roll(n int) int {
return rand.Intn(n) return rand.Intn(n)
} }
// RollInt63n returns a non-negative number between 0 (inclusive) and n (exclusive).
func RollInt63n(n int64) int64 {
if n == 1 {
return 0
}
return rand.Int63n(n)
}
// Roll returns a non-negative number between 0 (inclusive) and n (exclusive). // Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
func RollDeterministic(n int, seed int64) int { func RollDeterministic(n int, seed int64) int {
if n == 1 { if n == 1 {