From 14eb8967be7acc54c5dc9a416151ac45c01251b6 Mon Sep 17 00:00:00 2001 From: Martin HS Date: Thu, 21 Mar 2024 13:50:13 +0100 Subject: [PATCH] all: use min/max/clear from go1.21 (#29307) --- accounts/keystore/keystore.go | 4 +--- core/vm/analysis_test.go | 4 +--- crypto/crypto.go | 4 +--- crypto/secp256k1/scalar_mult_cgo.go | 8 ++------ eth/protocols/eth/peer.go | 8 -------- internal/era/era.go | 9 +-------- metrics/sample_test.go | 7 ------- p2p/discover/common.go | 7 ------- p2p/discover/v5wire/crypto.go | 4 +--- rlp/decode.go | 4 +--- 10 files changed, 8 insertions(+), 51 deletions(-) diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index e62a8eb257..5c978cf0b4 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -500,7 +500,5 @@ func (ks *KeyStore) isUpdating() bool { // zeroKey zeroes a private key in memory. func zeroKey(k *ecdsa.PrivateKey) { b := k.D.Bits() - for i := range b { - b[i] = 0 - } + clear(b) } diff --git a/core/vm/analysis_test.go b/core/vm/analysis_test.go index 398861f8ae..471d2b4ffb 100644 --- a/core/vm/analysis_test.go +++ b/core/vm/analysis_test.go @@ -93,9 +93,7 @@ func BenchmarkJumpdestOpAnalysis(bench *testing.B) { bits := make(bitvec, len(code)/8+1+4) b.ResetTimer() for i := 0; i < b.N; i++ { - for j := range bits { - bits[j] = 0 - } + clear(bits) codeBitmapInternal(code, bits) } } diff --git a/crypto/crypto.go b/crypto/crypto.go index 734feed5ca..7f7171f730 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -287,7 +287,5 @@ func PubkeyToAddress(p ecdsa.PublicKey) common.Address { } func zeroBytes(bytes []byte) { - for i := range bytes { - bytes[i] = 0 - } + clear(bytes) } diff --git a/crypto/secp256k1/scalar_mult_cgo.go b/crypto/secp256k1/scalar_mult_cgo.go index 8afa9d023b..bdf8eeede7 100644 --- a/crypto/secp256k1/scalar_mult_cgo.go +++ b/crypto/secp256k1/scalar_mult_cgo.go @@ -44,12 +44,8 @@ func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, // Unpack the result and clear temporaries. x := new(big.Int).SetBytes(point[:32]) y := new(big.Int).SetBytes(point[32:]) - for i := range point { - point[i] = 0 - } - for i := range padded { - scalar[i] = 0 - } + clear(point) + clear(scalar) if res != 1 { return nil, nil } diff --git a/eth/protocols/eth/peer.go b/eth/protocols/eth/peer.go index 94f28f240f..f53782a053 100644 --- a/eth/protocols/eth/peer.go +++ b/eth/protocols/eth/peer.go @@ -42,14 +42,6 @@ const ( maxQueuedTxAnns = 4096 ) -// max is a helper function which returns the larger of the two given integers. -func max(a, b int) int { - if a > b { - return a - } - return b -} - // Peer is a collection of relevant information we have about a `eth` peer. type Peer struct { id string // Unique ID for the peer, cached diff --git a/internal/era/era.go b/internal/era/era.go index 2099c2d575..22715a82e5 100644 --- a/internal/era/era.go +++ b/internal/era/era.go @@ -229,7 +229,7 @@ func (e *Era) readOffset(n uint64) (int64, error) { ) e.mu.Lock() defer e.mu.Unlock() - clearBuffer(e.buf[:]) + clear(e.buf[:]) if _, err := e.f.ReadAt(e.buf[:], offOffset); err != nil { return 0, err } @@ -248,13 +248,6 @@ func newSnappyReader(e *e2store.Reader, expectedType uint16, off int64) (io.Read return snappy.NewReader(r), int64(n), err } -// clearBuffer zeroes out the buffer. -func clearBuffer(buf []byte) { - for i := 0; i < len(buf); i++ { - buf[i] = 0 - } -} - // metadata wraps the metadata in the block index. type metadata struct { start uint64 diff --git a/metrics/sample_test.go b/metrics/sample_test.go index 7967357055..9835ec1c30 100644 --- a/metrics/sample_test.go +++ b/metrics/sample_test.go @@ -87,13 +87,6 @@ func BenchmarkUniformSample1028(b *testing.B) { benchmarkSample(b, NewUniformSample(1028)) } -func min(a, b int) int { - if a < b { - return a - } - return b -} - func TestExpDecaySample(t *testing.T) { for _, tc := range []struct { reservoirSize int diff --git a/p2p/discover/common.go b/p2p/discover/common.go index c9f0477def..1f763904bb 100644 --- a/p2p/discover/common.go +++ b/p2p/discover/common.go @@ -92,10 +92,3 @@ type ReadPacket struct { Data []byte Addr *net.UDPAddr } - -func min(x, y int) int { - if x > y { - return y - } - return x -} diff --git a/p2p/discover/v5wire/crypto.go b/p2p/discover/v5wire/crypto.go index fc0a0edef5..00fc3b4564 100644 --- a/p2p/discover/v5wire/crypto.go +++ b/p2p/discover/v5wire/crypto.go @@ -129,9 +129,7 @@ func deriveKeys(hash hashFn, priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey, n1, n sec := session{writeKey: make([]byte, aesKeySize), readKey: make([]byte, aesKeySize)} kdf.Read(sec.writeKey) kdf.Read(sec.readKey) - for i := range eph { - eph[i] = 0 - } + clear(eph) return &sec } diff --git a/rlp/decode.go b/rlp/decode.go index 9b17d2d810..47801b2090 100644 --- a/rlp/decode.go +++ b/rlp/decode.go @@ -1105,9 +1105,7 @@ func (s *Stream) readUint(size byte) (uint64, error) { return uint64(b), err default: buffer := s.uintbuf[:8] - for i := range buffer { - buffer[i] = 0 - } + clear(buffer) start := int(8 - size) if err := s.readFull(buffer[start:]); err != nil { return 0, err