Skip to content

Commit d8b68e3

Browse files
committed
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/[email protected]#Read - https://pkg.go.dev/math/[email protected]#Seed Signed-off-by: Akihiro Suda <[email protected]>
1 parent a9ac5f9 commit d8b68e3

17 files changed

Lines changed: 80 additions & 29 deletions

File tree

archive/compression/compression_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"bytes"
2121
"compress/gzip"
2222
"context"
23+
"crypto/rand"
2324
"io"
24-
"math/rand"
2525
"os"
2626
"path/filepath"
2727
"runtime"

cmd/containerd/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ import (
2323

2424
"github.com/containerd/containerd/cmd/containerd/command"
2525
"github.com/containerd/containerd/pkg/hasher"
26-
"github.com/containerd/containerd/pkg/seed"
26+
"github.com/containerd/containerd/pkg/seed" //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
2727

2828
_ "github.com/containerd/containerd/cmd/containerd/builtins"
2929
)
3030

3131
func init() {
32+
//nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
3233
seed.WithTimeAndRand()
3334
crypto.RegisterHash(crypto.SHA256, hasher.NewSHA256)
3435
}

cmd/ctr/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ import (
2323

2424
"github.com/containerd/containerd/cmd/ctr/app"
2525
"github.com/containerd/containerd/pkg/hasher"
26-
"github.com/containerd/containerd/pkg/seed"
26+
"github.com/containerd/containerd/pkg/seed" //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
2727
"github.com/urfave/cli"
2828
)
2929

3030
var pluginCmds = []cli.Command{}
3131

3232
func init() {
33+
//nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
3334
seed.WithTimeAndRand()
3435
crypto.RegisterHash(crypto.SHA256, hasher.NewSHA256)
3536
}

content/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24-
"math/rand"
2524
"sync"
2625
"time"
2726

2827
"github.com/containerd/containerd/errdefs"
2928
"github.com/containerd/containerd/log"
29+
"github.com/containerd/containerd/pkg/randutil"
3030
"github.com/opencontainers/go-digest"
3131
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3232
)
@@ -123,7 +123,7 @@ func OpenWriter(ctx context.Context, cs Ingester, opts ...WriterOpt) (Writer, er
123123
// error or abort. Requires asserting for an ingest manager
124124

125125
select {
126-
case <-time.After(time.Millisecond * time.Duration(rand.Intn(retry))):
126+
case <-time.After(time.Millisecond * time.Duration(randutil.Intn(retry))):
127127
if retry < 2048 {
128128
retry = retry << 1
129129
}

content/local/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23-
"math/rand"
2423
"os"
2524
"path/filepath"
2625
"strconv"
@@ -32,6 +31,7 @@ import (
3231
"github.com/containerd/containerd/errdefs"
3332
"github.com/containerd/containerd/filters"
3433
"github.com/containerd/containerd/log"
34+
"github.com/containerd/containerd/pkg/randutil"
3535
"github.com/sirupsen/logrus"
3636

3737
"github.com/opencontainers/go-digest"
@@ -473,7 +473,7 @@ func (s *store) Writer(ctx context.Context, opts ...content.WriterOpt) (content.
473473
lockErr = nil
474474
break
475475
}
476-
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1<<count)))
476+
time.Sleep(time.Millisecond * time.Duration(randutil.Intn(1<<count)))
477477
}
478478

479479
if lockErr != nil {

content/local/store_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"bufio"
2121
"bytes"
2222
"context"
23+
"crypto/rand"
2324
_ "crypto/sha256" // required for digest package
2425
"fmt"
2526
"io"
26-
"math/rand"
2727
"os"
2828
"path/filepath"
2929
"reflect"
@@ -35,6 +35,7 @@ import (
3535
"github.com/containerd/containerd/content"
3636
"github.com/containerd/containerd/content/testsuite"
3737
"github.com/containerd/containerd/errdefs"
38+
"github.com/containerd/containerd/pkg/randutil"
3839
"github.com/containerd/containerd/pkg/testutil"
3940

4041
"github.com/opencontainers/go-digest"
@@ -268,7 +269,7 @@ func generateBlobs(t checker, nblobs, maxsize int64) map[digest.Digest][]byte {
268269
blobs := map[digest.Digest][]byte{}
269270

270271
for i := int64(0); i < nblobs; i++ {
271-
p := make([]byte, rand.Int63n(maxsize))
272+
p := make([]byte, randutil.Int63n(maxsize))
272273

273274
if _, err := rand.Read(p); err != nil {
274275
t.Fatal(err)

diff/walking/differ.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package walking
1818

1919
import (
2020
"context"
21+
"crypto/rand"
2122
"encoding/base64"
2223
"errors"
2324
"fmt"
2425
"io"
25-
"math/rand"
2626
"time"
2727

2828
"github.com/containerd/containerd/archive"

leases/id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package leases
1818

1919
import (
20+
"crypto/rand"
2021
"encoding/base64"
2122
"fmt"
22-
"math/rand"
2323
"time"
2424
)
2525

mount/losetup_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package mount
1919
import (
2020
"errors"
2121
"fmt"
22-
"math/rand"
2322
"os"
2423
"strings"
2524
"time"
2625

26+
"github.com/containerd/containerd/pkg/randutil"
2727
"golang.org/x/sys/unix"
2828
)
2929

@@ -152,7 +152,7 @@ func setupLoop(backingFile string, param LoopParams) (*os.File, error) {
152152
// with EBUSY when trying to set it up.
153153
if strings.Contains(err.Error(), ebusyString) {
154154
// Fallback a bit to avoid live lock
155-
time.Sleep(time.Millisecond * time.Duration(rand.Intn(retry*10)))
155+
time.Sleep(time.Millisecond * time.Duration(randutil.Intn(retry*10)))
156156
continue
157157
}
158158
return nil, err

pkg/cri/util/id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package util
1818

1919
import (
20+
"crypto/rand"
2021
"encoding/hex"
21-
"math/rand"
2222
)
2323

2424
// GenerateID generates a random unique id.

0 commit comments

Comments
 (0)