From d198040bff72804a8a2a108fa6aa4e969f2cd9e5 Mon Sep 17 00:00:00 2001 From: tony Date: Wed, 9 Oct 2024 17:26:00 +0800 Subject: [PATCH] update ignore directive --- api/accounts/accounts_test.go | 4 ++-- api/blocks/blocks_test.go | 2 +- api/debug/debug_test.go | 2 +- api/events/events_test.go | 2 +- api/metrics_test.go | 2 +- api/node/node_test.go | 2 +- api/transactions/transactions_test.go | 4 ++-- api/transfers/transfers_test.go | 2 +- block/header_test.go | 2 +- cache/prio_cache_test.go | 7 ++++--- cache/rnd_cache.go | 4 ++-- cmd/thor/solo/solo.go | 2 +- comm/peer.go | 2 +- log/format_test.go | 4 ++-- metrics/noop_test.go | 7 ++++--- metrics/prometheus_test.go | 9 +++++---- p2psrv/rpc/rpc.go | 2 +- runtime/statedb/statedb_test.go | 2 +- test/datagen/numbers.go | 6 +++--- thor/hash_test.go | 4 ++-- trie/iterator_test.go | 3 ++- trie/proof_test.go | 7 ++++--- trie/trie_test.go | 2 +- txpool/blocklist_test.go | 2 +- txpool/tx_object_test.go | 4 ++-- txpool/tx_pool.go | 2 +- 26 files changed, 48 insertions(+), 43 deletions(-) diff --git a/api/accounts/accounts_test.go b/api/accounts/accounts_test.go index d7b64140f..d9f66fa24 100644 --- a/api/accounts/accounts_test.go +++ b/api/accounts/accounts_test.go @@ -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)) + res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107 if err != nil { t.Fatal(err) } @@ -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) + res, err := http.Get(url) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/api/blocks/blocks_test.go b/api/blocks/blocks_test.go index 4173d2bd8..b8c9636fe 100644 --- a/api/blocks/blocks_test.go +++ b/api/blocks/blocks_test.go @@ -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) + res, err := http.Get(url) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/api/debug/debug_test.go b/api/debug/debug_test.go index 6e3b1aa0d..90a0d7ac1 100644 --- a/api/debug/debug_test.go +++ b/api/debug/debug_test.go @@ -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)) + res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/api/events/events_test.go b/api/events/events_test.go index 5126a18cc..b9d8c18d2 100644 --- a/api/events/events_test.go +++ b/api/events/events_test.go @@ -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)) + res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/api/metrics_test.go b/api/metrics_test.go index c1c918291..59e86c9c8 100644 --- a/api/metrics_test.go +++ b/api/metrics_test.go @@ -181,7 +181,7 @@ func TestWebsocketMetrics(t *testing.T) { } func httpGet(t *testing.T, url string) ([]byte, int) { - res, err := http.Get(url) + res, err := http.Get(url) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/api/node/node_test.go b/api/node/node_test.go index 7952415a3..9f9179ef1 100644 --- a/api/node/node_test.go +++ b/api/node/node_test.go @@ -56,7 +56,7 @@ func initCommServer(t *testing.T) { } func httpGet(t *testing.T, url string) []byte { - res, err := http.Get(url) + res, err := http.Get(url) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/api/transactions/transactions_test.go b/api/transactions/transactions_test.go index 8ab9c3354..0beada359 100644 --- a/api/transactions/transactions_test.go +++ b/api/transactions/transactions_test.go @@ -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)) + res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107 if err != nil { t.Fatal(err) } @@ -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) + res, err := http.Get(url) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/api/transfers/transfers_test.go b/api/transfers/transfers_test.go index 0061d7114..97d8d1238 100644 --- a/api/transfers/transfers_test.go +++ b/api/transfers/transfers_test.go @@ -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)) + res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) //#nosec G107 if err != nil { t.Fatal(err) } diff --git a/block/header_test.go b/block/header_test.go index 1e1aa5a68..0d611eac4 100644 --- a/block/header_test.go +++ b/block/header_test.go @@ -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() diff --git a/cache/prio_cache_test.go b/cache/prio_cache_test.go index 8dffdd367..d7105b379 100644 --- a/cache/prio_cache_test.go +++ b/cache/prio_cache_test.go @@ -3,6 +3,7 @@ // Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying // file LICENSE or +// #nosec G404 package cache_test import ( @@ -44,9 +45,9 @@ func TestPrioCache(t *testing.T) { for i := 0; i < 100; i++ { e := kvp{ - rand.Int(), // #nosec - rand.Int(), // #nosec - rand.Float64()} // #nosec + rand.Int(), + rand.Int(), + rand.Float64()} kvps = append(kvps, e) c.Set(e.k, e.v, e.p) } diff --git a/cache/rnd_cache.go b/cache/rnd_cache.go index 581840f2c..964ddf255 100644 --- a/cache/rnd_cache.go +++ b/cache/rnd_cache.go @@ -101,7 +101,7 @@ func (rc *RandCache) Pick() *Entry { if len(rc.s) == 0 { return nil } - ent := rc.s[rand.N(len(rc.s))] // #nosec + ent := rc.s[rand.N(len(rc.s))] //#nosec G404 cpy := ent.Entry return &cpy } @@ -136,6 +136,6 @@ func (rc *RandCache) randDrop() { if len(rc.s) == 0 { return } - ent := rc.s[rand.N(len(rc.s))] // #nosec + ent := rc.s[rand.N(len(rc.s))] //#nosec rc.remove(ent.Key) } diff --git a/cmd/thor/solo/solo.go b/cmd/thor/solo/solo.go index 2c1a6d3e3..638aa74ff 100644 --- a/cmd/thor/solo/solo.go +++ b/cmd/thor/solo/solo.go @@ -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() diff --git a/comm/peer.go b/comm/peer.go index 0cb21fe46..85d30e34c 100644 --- a/comm/peer.go +++ b/comm/peer.go @@ -80,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.N(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) diff --git a/log/format_test.go b/log/format_test.go index 878b705cb..7e48f6b99 100644 --- a/log/format_test.go +++ b/log/format_test.go @@ -27,7 +27,7 @@ func BenchmarkPrettyInt64Logfmt(b *testing.B) { buf := make([]byte, 100) b.ReportAllocs() for i := 0; i < b.N; i++ { - sink = appendInt64(buf, rand.Int64()) // #nosec + sink = appendInt64(buf, rand.Int64()) //#nosec G404 } } @@ -35,6 +35,6 @@ func BenchmarkPrettyUint64Logfmt(b *testing.B) { buf := make([]byte, 100) b.ReportAllocs() for i := 0; i < b.N; i++ { - sink = appendUint64(buf, rand.Uint64(), false) // #nosec + sink = appendUint64(buf, rand.Uint64(), false) //#nosec G404 } } diff --git a/metrics/noop_test.go b/metrics/noop_test.go index 2a975ff91..b3583895e 100644 --- a/metrics/noop_test.go +++ b/metrics/noop_test.go @@ -3,6 +3,7 @@ // Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying // file LICENSE or +// #nosec G404 package metrics import ( @@ -26,21 +27,21 @@ func TestNoopMetrics(t *testing.T) { Counter("count2") count1.Add(1) - randCount2 := rand.N(100) + 1 // #nosec + 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.N(100)+1; i++ { // #nosec + 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.N(100)+1; i++ { // #nosec + 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"}) } diff --git a/metrics/prometheus_test.go b/metrics/prometheus_test.go index 974f5430d..5e4e6a757 100644 --- a/metrics/prometheus_test.go +++ b/metrics/prometheus_test.go @@ -3,6 +3,7 @@ // Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying // file LICENSE or +// #nosec G404 package metrics import ( @@ -31,13 +32,13 @@ func TestPromMetrics(t *testing.T) { gaugeVec := GaugeVec("gaugeVec1", []string{"zeroOrOne"}) count1.Add(1) - randCount2 := rand.N(100) + 1 // #nosec + randCount2 := rand.N(100) + 1 for i := 0; i < randCount2; i++ { Counter("count2").Add(1) } histTotal := 0 - for i := 0; i < rand.N(100)+2; i++ { // #nosec + for i := 0; i < rand.N(100)+2; i++ { zeroOrOne := i % 2 hist.Observe(int64(i)) HistogramVec("hist2", []string{"zeroOrOne"}, nil). @@ -46,7 +47,7 @@ func TestPromMetrics(t *testing.T) { } totalCountVec := 0 - randCountVec := rand.N(100) + 2 // #nosec + 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)}) @@ -54,7 +55,7 @@ func TestPromMetrics(t *testing.T) { } totalGaugeVec := 0 - randGaugeVec := rand.N(100) + 2 // #nosec + 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)}) diff --git a/p2psrv/rpc/rpc.go b/p2psrv/rpc/rpc.go index e19185bbd..bd5776e02 100644 --- a/p2psrv/rpc/rpc.go +++ b/p2psrv/rpc/rpc.go @@ -153,7 +153,7 @@ func (r *RPC) prepareCall(msgCode uint64, onResult func(*p2p.Msg) error) uint32 r.lock.Lock() defer r.lock.Unlock() for { - id := rand.Uint32() // #nosec + id := rand.Uint32() //#nosec G404 if id == 0 { // 0 id is taken by Notify continue diff --git a/runtime/statedb/statedb_test.go b/runtime/statedb/statedb_test.go index ef1e25831..dd5fc8b35 100644 --- a/runtime/statedb/statedb_test.go +++ b/runtime/statedb/statedb_test.go @@ -134,7 +134,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction { nameargs = append(nameargs, addr.Hex()) } for _, i := range action.args { - action.args[i] = rand.Int63n(100) // #nosec + action.args[i] = rand.Int63n(100) //#nosec G404 nameargs = append(nameargs, fmt.Sprint(action.args[i])) } action.name += strings.Join(nameargs, ", ") diff --git a/test/datagen/numbers.go b/test/datagen/numbers.go index 84df41de5..9da0cdc00 100644 --- a/test/datagen/numbers.go +++ b/test/datagen/numbers.go @@ -6,13 +6,13 @@ package datagen import ( - mathrand "math/rand" + mathrand "math/rand/v2" ) func RandInt() int { - return mathrand.Int() // #nosec + return mathrand.Int() //#nosec G404 } func RandIntN(n int) int { - return mathrand.Intn(n) // #nosec + return mathrand.N(n) //#nosec G404 } diff --git a/thor/hash_test.go b/thor/hash_test.go index fc9432ffb..fda605a97 100644 --- a/thor/hash_test.go +++ b/thor/hash_test.go @@ -19,7 +19,7 @@ import ( func BenchmarkHash(b *testing.B) { data := make([]byte, 10) - rand.New(rand.NewSource(1)).Read(data) // #nosec + rand.New(rand.NewSource(1)).Read(data) //#nosec G404 b.Run("keccak", func(b *testing.B) { type keccakState interface { @@ -45,7 +45,7 @@ func BenchmarkHash(b *testing.B) { func BenchmarkBlake2b(b *testing.B) { data := make([]byte, 100) - rand.New(rand.NewSource(1)).Read(data) // #nosec + rand.New(rand.NewSource(1)).Read(data) //#nosec G404 b.Run("Blake2b", func(b *testing.B) { for i := 0; i < b.N; i++ { thor.Blake2b(data).Bytes() diff --git a/trie/iterator_test.go b/trie/iterator_test.go index 48b57345f..bddc99287 100644 --- a/trie/iterator_test.go +++ b/trie/iterator_test.go @@ -331,7 +331,8 @@ func TestIteratorContinueAfterError(t *testing.T) { // because that one is already loaded. var rkey []byte for { - if rkey = keys[rand.N(len(keys))]; !bytes.Equal(rkey, tr.Hash().Bytes()) { // #nosec + //#nosec G404 + if rkey = keys[rand.N(len(keys))]; !bytes.Equal(rkey, tr.Hash().Bytes()) { break } } diff --git a/trie/proof_test.go b/trie/proof_test.go index 09ccf32ae..40b972bf8 100644 --- a/trie/proof_test.go +++ b/trie/proof_test.go @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . +// #nosec G404 package trie import ( @@ -72,7 +73,7 @@ func TestVerifyBadProof(t *testing.T) { t.Fatal("zero length proof") } keys := proofs.Keys() - key := keys[mrand.N(len(keys))] // #nosec + key := keys[mrand.N(len(keys))] node, _ := proofs.Get(key) proofs.Delete(key) mutateByte(node) @@ -85,8 +86,8 @@ func TestVerifyBadProof(t *testing.T) { // mutateByte changes one byte in b. func mutateByte(b []byte) { - for r := mrand.N(len(b)); ; { // #nosec - new := byte(mrand.N(255)) // #nosec + for r := mrand.N(len(b)); ; { + new := byte(mrand.N(255)) if new != b[r] { b[r] = new break diff --git a/trie/trie_test.go b/trie/trie_test.go index 26e504bbc..78c1ce7ce 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -546,7 +546,7 @@ func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie { // insert into the trie before measuring the hashing. func BenchmarkHash(b *testing.B) { // Make the random benchmark deterministic - random := rand.New(rand.NewSource(0)) // #nosec + random := rand.New(rand.NewSource(0)) //#nosec G404 // Create a realistic account trie to hash addresses := make([][20]byte, b.N) diff --git a/txpool/blocklist_test.go b/txpool/blocklist_test.go index c85bda845..b63e80eff 100644 --- a/txpool/blocklist_test.go +++ b/txpool/blocklist_test.go @@ -26,7 +26,7 @@ func SetupTempFile(t *testing.T, dummyData string) string { } testFilePath := tempFile.Name() - err = os.WriteFile(testFilePath, []byte(dummyData), 0644) // #nosec + err = os.WriteFile(testFilePath, []byte(dummyData), 0600) if err != nil { t.Fatalf("Failed to write to temp file: %s", err) } diff --git a/txpool/tx_object_test.go b/txpool/tx_object_test.go index 99e391582..8358f1a6d 100644 --- a/txpool/tx_object_test.go +++ b/txpool/tx_object_test.go @@ -36,7 +36,7 @@ func newTx(chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx.BlockRef return tx.MustSign(builder.BlockRef(blockRef). Expiration(expiration). - Nonce(rand.Uint64()). // #nosec + Nonce(rand.Uint64()). //#nosec G404 DependsOn(dependsOn). Features(features). Gas(gas). @@ -56,7 +56,7 @@ func newDelegatedTx(chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx trx := builder.BlockRef(blockRef). Expiration(expiration). - Nonce(rand.Uint64()). // #nosec + Nonce(rand.Uint64()). //#nosec G404 DependsOn(dependsOn). Features(features). Gas(gas). diff --git a/txpool/tx_pool.go b/txpool/tx_pool.go index 454411b9d..8493a8003 100644 --- a/txpool/tx_pool.go +++ b/txpool/tx_pool.go @@ -183,7 +183,7 @@ func (p *TxPool) fetchBlocklistLoop() { for { // delay 1~2 min - delay := time.Second * time.Duration(rand.Int()%60+60) // #nosec + delay := time.Second * time.Duration(rand.Int()%60+60) //#nosec G404 select { case <-p.ctx.Done(): return