Skip to content

Commit

Permalink
Merge pull request #1561 from ktock/rand
Browse files Browse the repository at this point in the history
Use crypto/rand instead of deprecated math/rand
  • Loading branch information
AkihiroSuda authored Feb 14, 2024
2 parents 1142ac4 + 51e4568 commit d7e16d4
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/containerd-stargz-grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type snapshotterConfig struct {
}

func main() {
rand.Seed(time.Now().UnixNano())
rand.Seed(time.Now().UnixNano()) //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
flag.Parse()
lvl, err := logrus.ParseLevel(*logLevel)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/stargz-store/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type KubeconfigKeychainConfig struct {
type ResolverConfig resolver.Config

func main() {
rand.Seed(time.Now().UnixNano())
rand.Seed(time.Now().UnixNano()) //nolint:staticcheck // Global math/rand seed is deprecated, but still used by external dependencies
flag.Parse()
mountPoint := flag.Arg(0)
lvl, err := logrus.ParseLevel(*logLevel)
Expand Down
13 changes: 7 additions & 6 deletions estargz/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"math/big"
"os"
"path/filepath"
"reflect"
Expand All @@ -45,10 +46,6 @@ import (
digest "github.com/opencontainers/go-digest"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// TestingController is Compression with some helper methods necessary for testing.
type TestingController interface {
Compression
Expand Down Expand Up @@ -2293,7 +2290,11 @@ var runes = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX
func randomContents(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = runes[rand.Intn(len(runes))]
bi, err := rand.Int(rand.Reader, big.NewInt(int64(len(runes))))
if err != nil {
panic(err)
}
b[i] = runes[int(bi.Int64())]
}
return string(b)
}
Expand Down
11 changes: 8 additions & 3 deletions fs/layer/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ import (
"bytes"
"compress/gzip"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"math/rand"
"math"
"math/big"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -979,8 +981,11 @@ func hasStateFile(t *testing.T, id string) check {
}

// wanted data
rand.Seed(time.Now().UnixNano())
wantErr := fmt.Errorf("test-%d", rand.Int63())
b, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
panic(err)
}
wantErr := fmt.Errorf("test-%d", b.Int64())

// report the data
root.fs.s.report(wantErr)
Expand Down
9 changes: 7 additions & 2 deletions fs/remote/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ package remote

import (
"context"
"crypto/rand"
"crypto/sha256"
"fmt"
"io"
"math/rand"
"math/big"
"mime"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -165,7 +166,11 @@ func jitter(duration time.Duration) time.Duration {
if duration <= 0 {
return duration
}
return time.Duration(rand.Int63n(int64(duration)) + int64(duration))
b, err := rand.Int(rand.Reader, big.NewInt(int64(duration)))
if err != nil {
panic(err)
}
return time.Duration(b.Int64() + int64(duration))
}

// backoffStrategy extends retryablehttp's DefaultBackoff to add a random jitter to avoid overwhelming the repository
Expand Down
5 changes: 0 additions & 5 deletions metadata/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"compress/gzip"
"fmt"
"io"
"math/rand"
"os"
"path"
"path/filepath"
Expand All @@ -37,10 +36,6 @@ import (
digest "github.com/opencontainers/go-digest"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

var allowedPrefix = [4]string{"", "./", "/", "../"}

var srcCompressions = map[string]tutil.CompressionFactory{
Expand Down
2 changes: 1 addition & 1 deletion util/testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package testutil

import (
"math/rand"
"crypto/rand"
"testing"
)

Expand Down

0 comments on commit d7e16d4

Please sign in to comment.