From 6044398f34c6ef684edc429f1013e427bbc9931c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 14 Jul 2026 10:37:38 +0200 Subject: [PATCH] networkdb: rewrite with math/rand/v2 and avoid divide-by-zero While at it, also check for zero value to prevent a (theoretical) panic (panic: runtime error: integer divide by zero). Signed-off-by: Sebastiaan van Stijn --- daemon/libnetwork/networkdb/cluster.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/daemon/libnetwork/networkdb/cluster.go b/daemon/libnetwork/networkdb/cluster.go index d8f542dc60..afd9e282d5 100644 --- a/daemon/libnetwork/networkdb/cluster.go +++ b/daemon/libnetwork/networkdb/cluster.go @@ -6,7 +6,7 @@ import ( "encoding/hex" "fmt" golog "log" - rnd "math/rand" + "math/rand/v2" "net" "net/netip" "slices" @@ -234,12 +234,14 @@ func (nDB *NetworkDB) clusterLeave() error { } func (nDB *NetworkDB) triggerFunc(stagger time.Duration, C <-chan time.Time, f func()) { - // Use a random stagger to avoid synchronizing - randStagger := time.Duration(uint64(rnd.Int63()) % uint64(stagger)) //nolint:gosec // gosec complains about the use of rand here. It should be fine. - select { - case <-time.After(randStagger): - case <-nDB.ctx.Done(): - return + if stagger > 0 { + // Use a random stagger to avoid synchronizing. + randStagger := time.Duration(rand.Int64N(int64(stagger))) // #nosec G404 -- use of math/rand/v2 is fine for this purpose. + select { + case <-time.After(randStagger): + case <-nDB.ctx.Done(): + return + } } for { select {