1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #42851 from thaJeztah/namesgenerator_nosprintf

pkg/namesgenerator: replace uses of fmt.Sprintf()
This commit is contained in:
Sebastiaan van Stijn 2021-09-16 21:26:12 +02:00 committed by GitHub
commit e952346c99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -1,8 +1,8 @@
package namesgenerator // import "github.com/docker/docker/pkg/namesgenerator"
import (
"fmt"
"math/rand"
"strconv"
)
var (
@ -840,13 +840,13 @@ var (
// integer between 0 and 10 will be added to the end of the name, e.g `focused_turing3`
func GetRandomName(retry int) string {
begin:
name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))]) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand)
name := left[rand.Intn(len(left))] + "_" + right[rand.Intn(len(right))] //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand)
if name == "boring_wozniak" /* Steve Wozniak is not boring */ {
goto begin
}
if retry > 0 {
name = fmt.Sprintf("%s%d", name, rand.Intn(10)) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand)
name += strconv.Itoa(rand.Intn(10)) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand)
}
return name
}

View file

@ -25,3 +25,12 @@ func TestNameRetries(t *testing.T) {
}
}
func BenchmarkGetRandomName(b *testing.B) {
b.ReportAllocs()
var out string
for n := 0; n < b.N; n++ {
out = GetRandomName(5)
}
b.Log("Last result:", out)
}