Skip to content

Commit

Permalink
switch to math/rand/v2 (#852)
Browse files Browse the repository at this point in the history
* use math/rand/v2 package

* enable gosec in golanglint-ci

* update ignore directive
  • Loading branch information
libotony authored Oct 11, 2024
1 parent a0e5a44 commit 175050c
Show file tree
Hide file tree
Showing 30 changed files with 74 additions and 90 deletions.
4 changes: 2 additions & 2 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint:gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand All @@ -552,7 +552,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func checkExpandedBlock(t *testing.T, expBl *block.Block, actBl *blocks.JSONExpa
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func httpPostAndCheckResponseStatus(t *testing.T, url string, obj interface{}, r
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint:gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint:gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestWebsocketMetrics(t *testing.T) {
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func initCommServer(t *testing.T) {
}

func httpGet(t *testing.T, url string) []byte {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func httpPostAndCheckResponseStatus(t *testing.T, url string, obj interface{}, r
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint: gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -358,7 +358,7 @@ func checkMatchingTx(t *testing.T, expectedTx *tx.Transaction, actualTx *transac
}

func httpGetAndCheckResponseStatus(t *testing.T, url string, responseStatusCode int) []byte {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/transfers/transfers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint: gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107
if err != nil {
t.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions block/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package block

import (
"math/rand"
"crypto/rand"
"sync/atomic"
"testing"

Expand Down Expand Up @@ -63,7 +63,7 @@ func TestHeader_BetterThan(t *testing.T) {

func TestHeaderEncoding(t *testing.T) {
var sig [65]byte
rand.Read(sig[:]) // nolint
rand.Read(sig[:])

block := new(Builder).Build().WithSignature(sig[:])
h := block.Header()
Expand All @@ -86,8 +86,8 @@ func TestHeaderEncoding(t *testing.T) {

var proof [81]byte
var alpha [32]byte
rand.Read(proof[:]) // nolint
rand.Read(alpha[:]) // nolint
rand.Read(proof[:])
rand.Read(alpha[:])

cplx, err := NewComplexSignature(sig[:], proof[:])
if err != nil {
Expand All @@ -110,7 +110,7 @@ func TestHeaderEncoding(t *testing.T) {
// type extension struct{Alpha []byte}
func TestEncodingBadExtension(t *testing.T) {
var sig [65]byte
rand.Read(sig[:]) // nolint
rand.Read(sig[:])

block := new(Builder).Build().WithSignature(sig[:])
h := block.Header()
Expand Down Expand Up @@ -157,8 +157,8 @@ func TestEncodingBadExtension(t *testing.T) {
func TestEncodingExtension(t *testing.T) {
var sig [ComplexSigSize]byte
var alpha [32]byte
rand.Read(sig[:]) // nolint
rand.Read(alpha[:]) // nolint
rand.Read(sig[:])
rand.Read(alpha[:])

block := new(Builder).Alpha(alpha[:]).Build().WithSignature(sig[:])
h := block.Header()
Expand Down
11 changes: 5 additions & 6 deletions cache/prio_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

// #nosec G404
package cache_test

import (
"math/rand"
"math/rand/v2"
"sort"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/cache"
Expand All @@ -35,7 +35,6 @@ func TestPrioCacheAddRemove(t *testing.T) {

func TestPrioCache(t *testing.T) {
c := cache.NewPrioCache(5)
rand.Seed(time.Now().UnixNano()) // nolint:staticcheck

type kvp struct {
k, v int
Expand All @@ -46,9 +45,9 @@ func TestPrioCache(t *testing.T) {

for i := 0; i < 100; i++ {
e := kvp{
rand.Int(), // nolint: gosec
rand.Int(), // nolint:gosec
rand.Float64()} // nolint:gosec
rand.Int(),
rand.Int(),
rand.Float64()}
kvps = append(kvps, e)
c.Set(e.k, e.v, e.p)
}
Expand Down
11 changes: 3 additions & 8 deletions cache/rnd_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
package cache

import (
"math/rand"
"math/rand/v2"
"sync"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano()) // nolint:staticcheck
}

// RandCache a simple cache which randomly evicts entries when
// length exceeds limit.
type RandCache struct {
Expand Down Expand Up @@ -106,7 +101,7 @@ func (rc *RandCache) Pick() *Entry {
if len(rc.s) == 0 {
return nil
}
ent := rc.s[rand.Intn(len(rc.s))] // #nosec
ent := rc.s[rand.N(len(rc.s))] //#nosec G404
cpy := ent.Entry
return &cpy
}
Expand Down Expand Up @@ -141,6 +136,6 @@ func (rc *RandCache) randDrop() {
if len(rc.s) == 0 {
return
}
ent := rc.s[rand.Intn(len(rc.s))] // #nosec
ent := rc.s[rand.N(len(rc.s))] //#nosec
rc.remove(ent.Key)
}
5 changes: 2 additions & 3 deletions cmd/thor/node/tx_stash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package node

import (
"bytes"
"math/rand"
"math/rand/v2"
"sort"
"testing"

Expand All @@ -20,8 +20,7 @@ import (

func newTx() *tx.Transaction {
return tx.MustSign(
new(tx.Builder).
Nonce(rand.Uint64()).Build(), // nolint:gosec,
new(tx.Builder).Nonce(rand.Uint64()).Build(), //#nosec
genesis.DevAccounts()[0].PrivateKey,
)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/thor/solo/solo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"fmt"
"math/big"
"math/rand"
"math/rand/v2"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -262,7 +262,7 @@ func (s *Solo) newTx(clauses []*tx.Clause, from genesis.DevAccount) (*tx.Transac

trx := builder.BlockRef(tx.NewBlockRef(0)).
Expiration(math.MaxUint32).
Nonce(rand.Uint64()). // #nosec
Nonce(rand.Uint64()). //#nosec G404
DependsOn(nil).
Gas(1_000_000).
Build()
Expand Down
8 changes: 2 additions & 6 deletions comm/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package comm

import (
"math/rand"
"math/rand/v2"
"sync"
"time"

Expand All @@ -24,10 +24,6 @@ const (
maxKnownBlocks = 1024 // Maximum block IDs to keep in the known list (prevent DOS)
)

func init() {
rand.Seed(time.Now().UnixNano()) // nolint:staticcheck
}

// Peer extends p2p.Peer with RPC integrated.
type Peer struct {
*p2p.Peer
Expand Down Expand Up @@ -84,7 +80,7 @@ func (p *Peer) UpdateHead(id thor.Bytes32, totalScore uint64) {
// MarkTransaction marks a transaction to known.
func (p *Peer) MarkTransaction(hash thor.Bytes32) {
// that's 10~100 block intervals
expiration := mclock.AbsTime(time.Second * time.Duration(thor.BlockInterval*uint64(rand.Intn(91)+10))) // #nosec
expiration := mclock.AbsTime(time.Second * time.Duration(thor.BlockInterval*uint64(rand.N(91)+10))) //#nosec G404

deadline := mclock.Now() + expiration
p.knownTxs.Add(hash, deadline)
Expand Down
6 changes: 3 additions & 3 deletions log/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package log

import (
"math/rand"
"math/rand/v2"
"testing"
)

Expand All @@ -27,14 +27,14 @@ func BenchmarkPrettyInt64Logfmt(b *testing.B) {
buf := make([]byte, 100)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sink = appendInt64(buf, rand.Int63()) // nolint
sink = appendInt64(buf, rand.Int64()) //#nosec G404
}
}

func BenchmarkPrettyUint64Logfmt(b *testing.B) {
buf := make([]byte, 100)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sink = appendUint64(buf, rand.Uint64(), false) // nolint
sink = appendUint64(buf, rand.Uint64(), false) //#nosec G404
}
}
9 changes: 5 additions & 4 deletions metrics/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

// #nosec G404
package metrics

import (
"math/rand"
"math/rand/v2"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -26,21 +27,21 @@ func TestNoopMetrics(t *testing.T) {
Counter("count2")

count1.Add(1)
randCount2 := rand.Intn(100) + 1 // nolint:gosec
randCount2 := rand.N(100) + 1
for i := 0; i < randCount2; i++ {
Counter("count2").Add(1)
}

hist := Histogram("hist1", nil)
histVect := HistogramVec("hist2", []string{"zeroOrOne"}, nil)
for i := 0; i < rand.Intn(100)+1; i++ { // nolint:gosec
for i := 0; i < rand.N(100)+1; i++ {
hist.Observe(int64(i))
histVect.ObserveWithLabels(int64(i), map[string]string{"thisIsNonsense": "butDoesntBreak"})
}

countVect := CounterVec("countVec1", []string{"zeroOrOne"})
gaugeVec := GaugeVec("gaugeVec1", []string{"zeroOrOne"})
for i := 0; i < rand.Intn(100)+1; i++ { // nolint:gosec
for i := 0; i < rand.N(100)+1; i++ {
countVect.AddWithLabel(int64(i), map[string]string{"thisIsNonsense": "butDoesntBreak"})
gaugeVec.AddWithLabel(int64(i), map[string]string{"thisIsNonsense": "butDoesntBreak"})
}
Expand Down
11 changes: 6 additions & 5 deletions metrics/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

// #nosec G404
package metrics

import (
"math/rand"
"math/rand/v2"
"strconv"
"testing"

Expand All @@ -31,13 +32,13 @@ func TestPromMetrics(t *testing.T) {
gaugeVec := GaugeVec("gaugeVec1", []string{"zeroOrOne"})

count1.Add(1)
randCount2 := rand.Intn(100) + 1 // nolint:gosec
randCount2 := rand.N(100) + 1
for i := 0; i < randCount2; i++ {
Counter("count2").Add(1)
}

histTotal := 0
for i := 0; i < rand.Intn(100)+2; i++ { // nolint:gosec
for i := 0; i < rand.N(100)+2; i++ {
zeroOrOne := i % 2
hist.Observe(int64(i))
HistogramVec("hist2", []string{"zeroOrOne"}, nil).
Expand All @@ -46,15 +47,15 @@ func TestPromMetrics(t *testing.T) {
}

totalCountVec := 0
randCountVec := rand.Intn(100) + 2 // nolint:gosec
randCountVec := rand.N(100) + 2
for i := 0; i < randCountVec; i++ {
zeroOrOne := i % 2
countVect.AddWithLabel(int64(i), map[string]string{"zeroOrOne": strconv.Itoa(zeroOrOne)})
totalCountVec += i
}

totalGaugeVec := 0
randGaugeVec := rand.Intn(100) + 2 // nolint:gosec
randGaugeVec := rand.N(100) + 2
for i := 0; i < randGaugeVec; i++ {
zeroOrOne := i % 2
gaugeVec.AddWithLabel(int64(i), map[string]string{"zeroOrOne": strconv.Itoa(zeroOrOne)})
Expand Down
Loading

0 comments on commit 175050c

Please sign in to comment.