Skip to content

Commit 81fa937

Browse files
AkihiroSudathaJeztah
authored andcommitted
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]> (cherry picked from commit d8b68e3) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 70dc11a commit 81fa937

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
@@ -21,10 +21,11 @@ import (
2121
"os"
2222

2323
"github.com/containerd/containerd/cmd/containerd/command"
24-
"github.com/containerd/containerd/pkg/seed"
24+
"github.com/containerd/containerd/pkg/seed" //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
2525
)
2626

2727
func init() {
28+
//nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
2829
seed.WithTimeAndRand()
2930
}
3031

cmd/ctr/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ import (
2121
"os"
2222

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

2828
var pluginCmds = []cli.Command{}
2929

3030
func init() {
31+
//nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
3132
seed.WithTimeAndRand()
3233
}
3334

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
)
@@ -113,7 +113,7 @@ func OpenWriter(ctx context.Context, cs Ingester, opts ...WriterOpt) (Writer, er
113113
// error or abort. Requires asserting for an ingest manager
114114

115115
select {
116-
case <-time.After(time.Millisecond * time.Duration(rand.Intn(retry))):
116+
case <-time.After(time.Millisecond * time.Duration(randutil.Intn(retry))):
117117
if retry < 2048 {
118118
retry = retry << 1
119119
}

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,13 +19,13 @@ package mount
1919
import (
2020
"errors"
2121
"fmt"
22-
"math/rand"
2322
"os"
2423
"strings"
2524
"syscall"
2625
"time"
2726
"unsafe"
2827

28+
"github.com/containerd/containerd/pkg/randutil"
2929
"golang.org/x/sys/unix"
3030
)
3131

@@ -163,7 +163,7 @@ func setupLoop(backingFile string, param LoopParams) (*os.File, error) {
163163
// with EBUSY when trying to set it up.
164164
if strings.Contains(err.Error(), ebusyString) {
165165
// Fallback a bit to avoid live lock
166-
time.Sleep(time.Millisecond * time.Duration(rand.Intn(retry*10)))
166+
time.Sleep(time.Millisecond * time.Duration(randutil.Intn(retry*10)))
167167
continue
168168
}
169169
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)