From d8b68e3ccc2c859f20f08041024af5be0565601b Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Tue, 14 Feb 2023 15:32:14 +0900 Subject: [PATCH] Stop using math/rand.Read and rand.Seed (deprecated in Go 1.20) From golangci-lint: > SA1019: rand.Read has been deprecated since Go 1.20 because it >shouldn't be used: For almost all use cases, crypto/rand.Read is more >appropriate. (staticcheck) > SA1019: rand.Seed has been deprecated since Go 1.20 and an alternative >has been available since Go 1.0: Programs that call Seed and then expect >a specific sequence of results from the global random source (using >functions such as Int) can be broken when a dependency changes how >much it consumes from the global random source. To avoid such breakages, >programs that need a specific result sequence should use >NewRand(NewSource(seed)) to obtain a random generator that other >packages cannot access. (staticcheck) See also: - https://pkg.go.dev/math/rand@go1.20#Read - https://pkg.go.dev/math/rand@go1.20#Seed Signed-off-by: Akihiro Suda --- archive/compression/compression_test.go | 2 +- cmd/containerd/main.go | 3 +- cmd/ctr/main.go | 3 +- content/helpers.go | 4 +-- content/local/store.go | 4 +-- content/local/store_test.go | 5 +-- diff/walking/differ.go | 2 +- leases/id.go | 2 +- mount/losetup_linux.go | 4 +-- pkg/cri/util/id.go | 2 +- pkg/kmutex/kmutex_test.go | 13 +++---- pkg/randutil/randutil.go | 48 +++++++++++++++++++++++++ pkg/seed/seed.go | 5 +++ pkg/unpack/unpacker.go | 2 +- rootfs/apply.go | 2 +- snapshots/testsuite/helpers.go | 4 +-- snapshots/testsuite/testsuite.go | 4 +-- 17 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 pkg/randutil/randutil.go diff --git a/archive/compression/compression_test.go b/archive/compression/compression_test.go index 4de650335..fe1a62e35 100644 --- a/archive/compression/compression_test.go +++ b/archive/compression/compression_test.go @@ -20,8 +20,8 @@ import ( "bytes" "compress/gzip" "context" + "crypto/rand" "io" - "math/rand" "os" "path/filepath" "runtime" diff --git a/cmd/containerd/main.go b/cmd/containerd/main.go index 887e8158a..ccd41c3c0 100644 --- a/cmd/containerd/main.go +++ b/cmd/containerd/main.go @@ -23,12 +23,13 @@ import ( "github.com/containerd/containerd/cmd/containerd/command" "github.com/containerd/containerd/pkg/hasher" - "github.com/containerd/containerd/pkg/seed" + "github.com/containerd/containerd/pkg/seed" //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies _ "github.com/containerd/containerd/cmd/containerd/builtins" ) func init() { + //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies seed.WithTimeAndRand() crypto.RegisterHash(crypto.SHA256, hasher.NewSHA256) } diff --git a/cmd/ctr/main.go b/cmd/ctr/main.go index ca0def515..1845a73ed 100644 --- a/cmd/ctr/main.go +++ b/cmd/ctr/main.go @@ -23,13 +23,14 @@ import ( "github.com/containerd/containerd/cmd/ctr/app" "github.com/containerd/containerd/pkg/hasher" - "github.com/containerd/containerd/pkg/seed" + "github.com/containerd/containerd/pkg/seed" //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies "github.com/urfave/cli" ) var pluginCmds = []cli.Command{} func init() { + //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies seed.WithTimeAndRand() crypto.RegisterHash(crypto.SHA256, hasher.NewSHA256) } diff --git a/content/helpers.go b/content/helpers.go index f74d72a93..5404109a6 100644 --- a/content/helpers.go +++ b/content/helpers.go @@ -21,12 +21,12 @@ import ( "errors" "fmt" "io" - "math/rand" "sync" "time" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/log" + "github.com/containerd/containerd/pkg/randutil" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -123,7 +123,7 @@ func OpenWriter(ctx context.Context, cs Ingester, opts ...WriterOpt) (Writer, er // error or abort. Requires asserting for an ingest manager select { - case <-time.After(time.Millisecond * time.Duration(rand.Intn(retry))): + case <-time.After(time.Millisecond * time.Duration(randutil.Intn(retry))): if retry < 2048 { retry = retry << 1 } diff --git a/content/local/store.go b/content/local/store.go index 27aff14a4..baae3565b 100644 --- a/content/local/store.go +++ b/content/local/store.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "io" - "math/rand" "os" "path/filepath" "strconv" @@ -32,6 +31,7 @@ import ( "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/filters" "github.com/containerd/containerd/log" + "github.com/containerd/containerd/pkg/randutil" "github.com/sirupsen/logrus" "github.com/opencontainers/go-digest" @@ -473,7 +473,7 @@ func (s *store) Writer(ctx context.Context, opts ...content.WriterOpt) (content. lockErr = nil break } - time.Sleep(time.Millisecond * time.Duration(rand.Intn(1<