diff --git a/common/bitutil/bitutil.go b/common/bitutil/bitutil.go index a18a6d18ee..944f1f3b24 100644 --- a/common/bitutil/bitutil.go +++ b/common/bitutil/bitutil.go @@ -27,10 +27,7 @@ func XORBytes(dst, a, b []byte) int { // fastXORBytes xors in bulk. It only works on architectures that support // unaligned read/writes. func fastXORBytes(dst, a, b []byte) int { - n := len(a) - if len(b) < n { - n = len(b) - } + n := min(len(b), len(a)) w := n / wordSize if w > 0 { dw := *(*[]uintptr)(unsafe.Pointer(&dst)) @@ -49,10 +46,7 @@ func fastXORBytes(dst, a, b []byte) int { // safeXORBytes xors one by one. It works on all architectures, independent if // it supports unaligned read/writes or not. func safeXORBytes(dst, a, b []byte) int { - n := len(a) - if len(b) < n { - n = len(b) - } + n := min(len(b), len(a)) for i := 0; i < n; i++ { dst[i] = a[i] ^ b[i] } @@ -71,10 +65,7 @@ func ANDBytes(dst, a, b []byte) int { // fastANDBytes ands in bulk. It only works on architectures that support // unaligned read/writes. func fastANDBytes(dst, a, b []byte) int { - n := len(a) - if len(b) < n { - n = len(b) - } + n := min(len(b), len(a)) w := n / wordSize if w > 0 { dw := *(*[]uintptr)(unsafe.Pointer(&dst)) @@ -93,10 +84,7 @@ func fastANDBytes(dst, a, b []byte) int { // safeANDBytes ands one by one. It works on all architectures, independent if // it supports unaligned read/writes or not. func safeANDBytes(dst, a, b []byte) int { - n := len(a) - if len(b) < n { - n = len(b) - } + n := min(len(b), len(a)) for i := 0; i < n; i++ { dst[i] = a[i] & b[i] } @@ -115,10 +103,7 @@ func ORBytes(dst, a, b []byte) int { // fastORBytes ors in bulk. It only works on architectures that support // unaligned read/writes. func fastORBytes(dst, a, b []byte) int { - n := len(a) - if len(b) < n { - n = len(b) - } + n := min(len(b), len(a)) w := n / wordSize if w > 0 { dw := *(*[]uintptr)(unsafe.Pointer(&dst)) @@ -137,10 +122,7 @@ func fastORBytes(dst, a, b []byte) int { // safeORBytes ors one by one. It works on all architectures, independent if // it supports unaligned read/writes or not. func safeORBytes(dst, a, b []byte) int { - n := len(a) - if len(b) < n { - n = len(b) - } + n := min(len(b), len(a)) for i := 0; i < n; i++ { dst[i] = a[i] | b[i] } diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go index d3201850a8..c9258ea32c 100644 --- a/common/hexutil/hexutil.go +++ b/common/hexutil/hexutil.go @@ -147,10 +147,7 @@ func DecodeBig(input string) (*big.Int, error) { words := make([]big.Word, len(raw)/bigWordNibbles+1) end := len(raw) for i := range words { - start := end - bigWordNibbles - if start < 0 { - start = 0 - } + start := max(end-bigWordNibbles, 0) for ri := start; ri < end; ri++ { nib := decodeNibble(raw[ri]) if nib == badNibble { diff --git a/common/hexutil/json.go b/common/hexutil/json.go index e0ac98f52d..c423791430 100644 --- a/common/hexutil/json.go +++ b/common/hexutil/json.go @@ -179,10 +179,7 @@ func (b *Big) UnmarshalText(input []byte) error { words := make([]big.Word, len(raw)/bigWordNibbles+1) end := len(raw) for i := range words { - start := end - bigWordNibbles - if start < 0 { - start = 0 - } + start := max(end-bigWordNibbles, 0) for ri := start; ri < end; ri++ { nib := decodeNibble(raw[ri]) if nib == badNibble {