From 19330fc1081f641442312c96ed3197e2f9e129ce Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 20 Sep 2024 20:30:23 +0200 Subject: [PATCH] Refactor random number generation and add version-specific implementations Removed error handling from `randNum` in `random_test.go` and introduced new `randNum` implementations for different Go versions (1.19, 1.20-1.21, 1.22+). This ensures compatibility with different versions of Go and utilizes the appropriate version-specific random number generation methods. We are using math/rand instead crypto/rand. For our needs this should be sufficient. --- random.go | 22 ---------------------- random_119.go | 22 ++++++++++++++++++++++ random_121.go | 20 ++++++++++++++++++++ random_122.go | 22 ++++++++++++++++++++++ random_test.go | 8 +------- 5 files changed, 65 insertions(+), 29 deletions(-) create mode 100644 random_119.go create mode 100644 random_121.go create mode 100644 random_122.go diff --git a/random.go b/random.go index 831b118..3a3f16b 100644 --- a/random.go +++ b/random.go @@ -7,8 +7,6 @@ package mail import ( "crypto/rand" "encoding/binary" - "fmt" - "math/big" "strings" ) @@ -52,23 +50,3 @@ func randomStringSecure(length int) (string, error) { return randString.String(), nil } - -// randNum returns a random number with a maximum value of length -func randNum(length int) (int, error) { - if length <= 0 { - return 0, fmt.Errorf("provided number is <= 0: %d", length) - } - length64 := big.NewInt(int64(length)) - if !length64.IsUint64() { - return 0, fmt.Errorf("big.NewInt() generation returned negative value: %d", length64) - } - randNum64, err := rand.Int(rand.Reader, length64) - if err != nil { - return 0, err - } - randomNum := int(randNum64.Int64()) - if randomNum < 0 { - return 0, fmt.Errorf("generated random number does not fit as int64: %d", randNum64) - } - return randomNum, nil -} diff --git a/random_119.go b/random_119.go new file mode 100644 index 0000000..b084305 --- /dev/null +++ b/random_119.go @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors +// +// SPDX-License-Identifier: MIT + +//go:build go1.19 && !go1.20 +// +build go1.19,!go1.20 + +package mail + +import ( + "math/rand" + "time" +) + +// randNum returns a random number with a maximum value of length +func randNum(maxval int) int { + if maxval <= 0 { + return 0 + } + rand.Seed(time.Now().UnixNano()) + return rand.Intn(maxval) +} diff --git a/random_121.go b/random_121.go new file mode 100644 index 0000000..b401bc8 --- /dev/null +++ b/random_121.go @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors +// +// SPDX-License-Identifier: MIT + +//go:build go1.20 && !go1.22 +// +build go1.20,!go1.22 + +package mail + +import ( + "math/rand" +) + +// randNum returns a random number with a maximum value of length +func randNum(maxval int) int { + if maxval <= 0 { + return 0 + } + return rand.Intn(maxval) +} diff --git a/random_122.go b/random_122.go new file mode 100644 index 0000000..9a6132b --- /dev/null +++ b/random_122.go @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors +// +// SPDX-License-Identifier: MIT + +//go:build go1.22 +// +build go1.22 + +package mail + +import ( + "math/rand/v2" +) + +// randNum returns a random number with a maximum value of maxval. +// go-mail compiled with Go 1.22+ will make use of the novel math/rand/v2 interface +// Older versions of Go will use math/rand +func randNum(maxval int) int { + if maxval <= 0 { + return 0 + } + return rand.IntN(maxval) +} diff --git a/random_test.go b/random_test.go index 51e3ba6..caa5e58 100644 --- a/random_test.go +++ b/random_test.go @@ -55,13 +55,7 @@ func TestRandomNum(t *testing.T) { for _, tc := range tt { t.Run(tc.testName, func(t *testing.T) { - rn, err := randNum(tc.max) - if err != nil { - t.Errorf("random number generation failed: %s", err) - } - if rn < 0 { - t.Errorf("random number generation failed: %d is smaller than zero", rn) - } + rn := randNum(tc.max) if rn > tc.max { t.Errorf("random number generation failed: %d is bigger than given value %d", rn, tc.max) }