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

resolved the gosec issue failing unit tests #3358

Merged
Merged
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
10 changes: 7 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"archive/zip"
"bufio"
"context"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"math/rand"
"math/big"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -81,10 +82,13 @@ func ConvertLabelsToSelector(labels map[string]string) string {
// GenerateRandomString generates a random string of lower case characters of
// the given size
func GenerateRandomString(n int) string {
rand.Seed(time.Now().UnixNano())
b := make([]rune, n)

for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
// this error is ignored because it fails only when the 2nd arg of Int() is less then 0
// which wont happen
n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes))))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am quite sure this shouldn't overflow, as per the docs it shouldn't https://golang.org/pkg/crypto/rand/#Int

b[i] = letterRunes[n.Int64()]
}
return string(b)
}
Expand Down
13 changes: 2 additions & 11 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
Expand All @@ -14,20 +13,12 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/openshift/odo/pkg/util"
)

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

// RandString returns a random string of given length
func RandString(n int) string {
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
return util.GenerateRandomString(n)
}

// WaitForCmdOut runs a command until it gets
Expand Down