From a81ec225dce93440843f8d878498208e57534f64 Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Fri, 11 Oct 2024 20:16:26 -0700 Subject: [PATCH] Remove unnecessary calls to rand.Seed(time.Now().Unix()). As per https://pkg.go.dev/math/rand#Seed: "If Seed is not called, the generator is seeded randomly at program startup." "Prior to Go 1.20, the generator was seeded like Seed(1) at program startup. To force the old behavior, call Seed(1) at program startup." "As of Go 1.20 there is no reason to call Seed with a random value." rand.Seed() is deprecated. Followup to #11015. PiperOrigin-RevId: 685052229 --- pkg/compressio/compressio_test.go | 2 -- pkg/compressio/nocompressio_test.go | 4 ---- pkg/state/statefile/statefile_test.go | 3 --- pkg/tcpip/sample/tun_tcp_connect/main.go | 4 ---- pkg/tcpip/sample/tun_tcp_echo/main.go | 4 ---- runsc/boot/loader.go | 6 ------ runsc/boot/loader_test.go | 1 - test/benchmarks/tcp/tcp_proxy.go | 4 ---- test/fsstress/fsstress_test.go | 5 ----- 9 files changed, 33 deletions(-) diff --git a/pkg/compressio/compressio_test.go b/pkg/compressio/compressio_test.go index 7b40a6f1d..02208c8ed 100644 --- a/pkg/compressio/compressio_test.go +++ b/pkg/compressio/compressio_test.go @@ -154,8 +154,6 @@ func doTest(t harness, opts testOpts) { var hashKey = []byte("01234567890123456789012345678901") func TestCompress(t *testing.T) { - rand.Seed(time.Now().Unix()) - var ( data = initTest(t, 10*1024*1024) data0 = data[:0] diff --git a/pkg/compressio/nocompressio_test.go b/pkg/compressio/nocompressio_test.go index 25c2ba322..ae2908a45 100644 --- a/pkg/compressio/nocompressio_test.go +++ b/pkg/compressio/nocompressio_test.go @@ -18,14 +18,10 @@ import ( "bytes" "fmt" "io" - "math/rand" "testing" - "time" ) func TestNoCompress(t *testing.T) { - rand.Seed(time.Now().Unix()) - var ( data = initTest(t, 10*1024*1024) data0 = data[:0] diff --git a/pkg/state/statefile/statefile_test.go b/pkg/state/statefile/statefile_test.go index af9049f4c..1e6423a9b 100644 --- a/pkg/state/statefile/statefile_test.go +++ b/pkg/state/statefile/statefile_test.go @@ -22,7 +22,6 @@ import ( "math/rand" "runtime" "testing" - "time" "gvisor.dev/gvisor/pkg/compressio" ) @@ -44,8 +43,6 @@ type testCase struct { } func TestStatefile(t *testing.T) { - rand.Seed(time.Now().Unix()) - compression := map[string]CompressionLevel{ "none": CompressionLevelNone, "compressed": CompressionLevelFlateBestSpeed, diff --git a/pkg/tcpip/sample/tun_tcp_connect/main.go b/pkg/tcpip/sample/tun_tcp_connect/main.go index fa47c233e..810f4c23d 100644 --- a/pkg/tcpip/sample/tun_tcp_connect/main.go +++ b/pkg/tcpip/sample/tun_tcp_connect/main.go @@ -45,11 +45,9 @@ import ( "bytes" "fmt" "log" - "math/rand" "net" "os" "strconv" - "time" "gvisor.dev/gvisor/pkg/rawfile" "gvisor.dev/gvisor/pkg/tcpip" @@ -100,8 +98,6 @@ func main() { remoteAddrName := os.Args[4] remotePortName := os.Args[5] - rand.Seed(time.Now().UnixNano()) - addr := tcpip.AddrFromSlice(net.ParseIP(addrName).To4()) remote := tcpip.FullAddress{ NIC: 1, diff --git a/pkg/tcpip/sample/tun_tcp_echo/main.go b/pkg/tcpip/sample/tun_tcp_echo/main.go index e53c7948d..2223a17d1 100644 --- a/pkg/tcpip/sample/tun_tcp_echo/main.go +++ b/pkg/tcpip/sample/tun_tcp_echo/main.go @@ -25,12 +25,10 @@ import ( "flag" "io" "log" - "math/rand" "net" "os" "strconv" "strings" - "time" "gvisor.dev/gvisor/pkg/rawfile" "gvisor.dev/gvisor/pkg/tcpip" @@ -113,8 +111,6 @@ func main() { addrName := flag.Arg(1) portName := flag.Arg(2) - rand.Seed(time.Now().UnixNano()) - // Parse the mac address. maddr, err := net.ParseMAC(*mac) if err != nil { diff --git a/runsc/boot/loader.go b/runsc/boot/loader.go index cc94d205c..f00810559 100644 --- a/runsc/boot/loader.go +++ b/runsc/boot/loader.go @@ -19,7 +19,6 @@ import ( "encoding/json" "errors" "fmt" - mrand "math/rand" "os" "runtime" "strconv" @@ -294,11 +293,6 @@ type FDMapping struct { Host int } -func init() { - // Initialize the random number generator. - mrand.Seed(gtime.Now().UnixNano()) -} - // Args are the arguments for New(). type Args struct { // Id is the sandbox ID. diff --git a/runsc/boot/loader_test.go b/runsc/boot/loader_test.go index 414e553c8..ab99ee5c2 100644 --- a/runsc/boot/loader_test.go +++ b/runsc/boot/loader_test.go @@ -41,7 +41,6 @@ import ( func init() { log.SetLevel(log.Debug) - rand.Seed(time.Now().UnixNano()) if err := fsgofer.OpenProcSelfFD("/proc/self/fd"); err != nil { panic(err) } diff --git a/test/benchmarks/tcp/tcp_proxy.go b/test/benchmarks/tcp/tcp_proxy.go index 47a4ec9fc..eef0a7f10 100644 --- a/test/benchmarks/tcp/tcp_proxy.go +++ b/test/benchmarks/tcp/tcp_proxy.go @@ -398,10 +398,6 @@ func main() { if *forward == "" { log.Fatalf("no forward provided") } - // Seed the random number generator to ensure that we are given MAC addresses that don't - // for the case of the client and server stack. - rand.Seed(time.Now().UTC().UnixNano()) - if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { diff --git a/test/fsstress/fsstress_test.go b/test/fsstress/fsstress_test.go index 120c1d204..4bef05d4a 100644 --- a/test/fsstress/fsstress_test.go +++ b/test/fsstress/fsstress_test.go @@ -24,17 +24,12 @@ import ( "strconv" "strings" "testing" - "time" "github.com/docker/docker/api/types/mount" "gvisor.dev/gvisor/pkg/test/dockerutil" "gvisor.dev/gvisor/pkg/test/testutil" ) -func init() { - rand.Seed(int64(time.Now().Nanosecond())) -} - func TestMain(m *testing.M) { dockerutil.EnsureSupportedDockerVersion() flag.Parse()