Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEPRECATION] Remove references to deprecated rand.Seed (#2650) #2950

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions pkg/fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/gopasspw/gopass/pkg/appdir"
"github.com/gopasspw/gopass/pkg/debug"
Expand Down Expand Up @@ -138,8 +137,6 @@ func IsEmptyDir(path string) (bool, error) {

// Shred overwrite the given file any number of times.
func Shred(path string, runs int) error {
rand.Seed(time.Now().UnixNano())

fh, err := os.OpenFile(path, os.O_WRONLY, 0o600)
if err != nil {
return fmt.Errorf("failed to open file %q: %w", path, err)
Expand Down
2 changes: 0 additions & 2 deletions pkg/gitconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/gopasspw/gopass/internal/set"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -274,7 +273,6 @@ func TestLoadFromEnv(t *testing.T) {
"core.timeout": "10",
}

rand.Seed(time.Now().Unix())
prefix := fmt.Sprintf("GPTEST%d", rand.Int31n(8192))

i := 0
Expand Down
16 changes: 9 additions & 7 deletions pkg/pwgen/pwgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package pwgen

import (
"bytes"
"crypto/rand"
crand "crypto/rand"
"fmt"
"io"
mrand "math/rand"
Copy link
Contributor Author

@lhardt lhardt Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This choice was questioned on orangekame's PR.

Howver, this aligns pwgen_test.go with the style found in rand.go, of the same package.

Moreover, it only affects 3 references in a single test

"math/rand"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -40,12 +40,14 @@ func TestPwgenCharset(t *testing.T) {
assert.Equal(t, "", GeneratePasswordCharsetCheck(4, "a"))
}

func TestPwgenNoCrand(t *testing.T) {
old := rand.Reader
rand.Reader = strings.NewReader("")
func TestPwgenNoCrandFallback(t *testing.T) {
oldFallback := randFallback
oldReader := crand.Reader
crand.Reader = strings.NewReader("")

defer func() {
rand.Reader = old
crand.Reader = oldReader
randFallback = oldFallback
}()

oldOut := os.Stdout
Expand All @@ -61,7 +63,7 @@ func TestPwgenNoCrand(t *testing.T) {
}()

// if we seed math/rand with 1789, the first "random number" will be 42
mrand.Seed(1789)
randFallback = rand.New(rand.NewSource(1789))

n := randomInteger(1024)

Expand Down
6 changes: 4 additions & 2 deletions pkg/pwgen/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (

func init() {
// seed math/rand in case we have to fall back to using it
rand.Seed(time.Now().Unix() + int64(os.Getpid()+os.Getppid()))
randFallback = rand.New(rand.NewSource(time.Now().Unix() + int64(os.Getpid()+os.Getppid())))
}

var randFallback *rand.Rand

func randomInteger(max int) int {
i, err := crand.Int(crand.Reader, big.NewInt(int64(max)))
if err == nil {
Expand All @@ -22,5 +24,5 @@ func randomInteger(max int) int {

fmt.Fprintln(os.Stderr, "WARNING: No crypto/rand available. Falling back to PRNG")

return rand.Intn(max)
return randFallback.Intn(max)
}