Skip to content

Commit 869801f

Browse files
committed
Remove unnecessary checks on a value that is already definitely an alphanum
Signed-off-by: Matt Butcher <[email protected]>
1 parent 864fea7 commit 869801f

4 files changed

+78
-35
lines changed

cryptorandomstringutils.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"math"
2323
"math/big"
24-
"regexp"
2524
"unicode"
2625
)
2726

@@ -102,24 +101,8 @@ func CryptoRandomAlphaNumeric(count int) (string, error) {
102101
if count == 0 {
103102
return "", nil
104103
}
105-
RandomString, err := CryptoRandom(count, 0, 0, true, true)
106-
if err != nil {
107-
return "", fmt.Errorf("Error: %s", err)
108-
}
109-
match, err := regexp.MatchString("([0-9]+)", RandomString)
110-
if err != nil {
111-
panic(err)
112-
}
113-
114-
if !match {
115-
//Get the position between 0 and the length of the string-1 to insert a random number
116-
position := getCryptoRandomInt(count)
117-
//Insert a random number between [0-9] in the position
118-
RandomString = RandomString[:position] + string('0' + getCryptoRandomInt(10)) + RandomString[position + 1:]
119-
return RandomString, err
120-
}
121-
return RandomString, err
122104

105+
return CryptoRandom(count, 0, 0, true, true)
123106
}
124107

125108
/*
@@ -204,7 +187,7 @@ func CryptoRandom(count int, start int, end int, letters bool, numbers bool, cha
204187
if chars == nil {
205188
ch = rune(getCryptoRandomInt(gap) + int64(start))
206189
} else {
207-
ch = chars[getCryptoRandomInt(gap) + int64(start)]
190+
ch = chars[getCryptoRandomInt(gap)+int64(start)]
208191
}
209192

210193
if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {

cryptorandomstringutils_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package goutils
22

33
import (
4+
"regexp"
5+
"strconv"
46
"testing"
57
"unicode/utf8"
68
)
@@ -74,3 +76,38 @@ func TestCryptoRandomAlphaNumeric(t *testing.T) {
7476
}
7577
}
7678
}
79+
80+
func TestCryptoRandAlphaNumeric_FuzzOnlyNumeric(t *testing.T) {
81+
82+
// Testing for a reported regression in which some versions produced
83+
// a predictably small set of chars.
84+
iters := 1000
85+
charlen := 0
86+
for i := 0; i < 16; i++ {
87+
numOnly := 0
88+
charlen++
89+
for i := 0; i < iters; i++ {
90+
out, err := CryptoRandomAlphaNumeric(charlen)
91+
println(out)
92+
if err != nil {
93+
t.Fatal("func failed to produce a random thinger")
94+
}
95+
if _, err := strconv.Atoi(out); err == nil {
96+
numOnly++
97+
}
98+
99+
m, err := regexp.MatchString("^[0-9a-zA-Z]+$", out)
100+
if err != nil {
101+
t.Fatal(err)
102+
}
103+
if !m {
104+
t.Fatal("Character is not alphanum")
105+
}
106+
}
107+
108+
if numOnly == iters {
109+
t.Fatalf("Got %d numeric-only random sequences", numOnly)
110+
}
111+
}
112+
113+
}

randomstringutils.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"math"
2222
"math/rand"
23-
"regexp"
2423
"time"
2524
"unicode"
2625
)
@@ -75,12 +74,10 @@ func RandomNumeric(count int) (string, error) {
7574

7675
/*
7776
RandomAlphabetic creates a random string whose length is the number of characters specified.
78-
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
77+
Characters will be chosen from the set of alphabetic characters.
7978
8079
Parameters:
8180
count - the length of random string to create
82-
letters - if true, generated string may include alphabetic characters
83-
numbers - if true, generated string may include numeric characters
8481
8582
Returns:
8683
string - the random string
@@ -106,19 +103,8 @@ func RandomAlphaNumeric(count int) (string, error) {
106103
if err != nil {
107104
return "", fmt.Errorf("Error: %s", err)
108105
}
109-
match, err := regexp.MatchString("([0-9]+)", RandomString)
110-
if err != nil {
111-
panic(err)
112-
}
113106

114-
if !match {
115-
//Get the position between 0 and the length of the string-1 to insert a random number
116-
position := rand.Intn(count)
117-
//Insert a random number between [0-9] in the position
118-
RandomString = RandomString[:position] + string('0'+rand.Intn(10)) + RandomString[position+1:]
119-
return RandomString, err
120-
}
121-
return RandomString, err
107+
return RandomString[:count], nil
122108

123109
}
124110

randomstringutils_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package goutils
33
import (
44
"fmt"
55
"math/rand"
6+
"regexp"
7+
"strconv"
68
"testing"
79
)
810

@@ -76,3 +78,38 @@ func ExampleRandomSeed() {
7678
// H_I;E
7779
// 2b2ca
7880
}
81+
82+
func TestRandAlphaNumeric_FuzzOnlyNumeric(t *testing.T) {
83+
84+
// Testing for a reported regression in which some versions produced
85+
// a predictably small set of chars.
86+
iters := 1000
87+
charlen := 0
88+
for i := 0; i < 16; i++ {
89+
numOnly := 0
90+
charlen++
91+
for i := 0; i < iters; i++ {
92+
out, err := RandomAlphaNumeric(charlen)
93+
println(out)
94+
if err != nil {
95+
t.Fatal("func failed to produce a random thinger")
96+
}
97+
if _, err := strconv.Atoi(out); err == nil {
98+
numOnly++
99+
}
100+
101+
m, err := regexp.MatchString("^[0-9a-zA-Z]+$", out)
102+
if err != nil {
103+
t.Fatal(err)
104+
}
105+
if !m {
106+
t.Fatal("Character is not alphanum")
107+
}
108+
}
109+
110+
if numOnly == iters {
111+
t.Fatalf("Got %d numeric-only random sequences", numOnly)
112+
}
113+
}
114+
115+
}

0 commit comments

Comments
 (0)