all: clean up minor issues found by staticcheck

Use bytes.Equal instead of comparing bytes.Compare results to zero,
avoid Yoda conditions, drop a redundant zero slice capacity, use
strings.ReplaceAll, build acme error strings with a strings.Builder,
and lowercase an error string in ocsp. No behavior changes.

Change-Id: I5d2b3fa5c241adc75e61be0f2d2d7af1eb9e8d66
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/803200
Reviewed-by: Roland Shoemaker <roland@golang.org>
Auto-Submit: Roland Shoemaker <roland@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
Brad Fitzpatrick
2026-07-21 14:30:59 -07:00
committed by Gopher Robot
parent ff03dafdb0
commit f2135b814c
9 changed files with 13 additions and 12 deletions
+1 -1
View File
@@ -734,7 +734,7 @@ func TestTLSALPN01ChallengeCert(t *testing.T) {
if !acmeExts[0].Critical {
t.Errorf("acmeExt.Critical = %v; want true", acmeExts[0].Critical)
}
if bytes.Compare(acmeExts[0].Value, extValue) != 0 {
if !bytes.Equal(acmeExts[0].Value, extValue) {
t.Errorf("acmeExt.Value = %v; want %v", acmeExts[0].Value, extValue)
}
+5 -4
View File
@@ -114,14 +114,15 @@ type Error struct {
}
func (e *Error) Error() string {
str := fmt.Sprintf("%d %s: %s", e.StatusCode, e.ProblemType, e.Detail)
var sb strings.Builder
fmt.Fprintf(&sb, "%d %s: %s", e.StatusCode, e.ProblemType, e.Detail)
if len(e.Subproblems) > 0 {
str += fmt.Sprintf("; subproblems:")
sb.WriteString("; subproblems:")
for _, sp := range e.Subproblems {
str += fmt.Sprintf("\n\t%s", sp)
fmt.Fprintf(&sb, "\n\t%s", sp)
}
}
return str
return sb.String()
}
// AuthorizationError indicates that an authorization for an identifier
+1 -1
View File
@@ -322,7 +322,7 @@ type Request struct {
func (req *Request) Marshal() ([]byte, error) {
hashAlg := getOIDFromHashAlgorithm(req.HashAlgorithm)
if hashAlg == nil {
return nil, errors.New("Unknown hash algorithm")
return nil, errors.New("unknown hash algorithm")
}
return asn1.Marshal(ocspRequest{
TBSRequest: tbsRequest{
+1 -1
View File
@@ -195,7 +195,7 @@ func TestOCSPRequest(t *testing.T) {
t.Fatal(err)
}
if bytes.Compare(expectedBytes, marshaledRequest) != 0 {
if !bytes.Equal(expectedBytes, marshaledRequest) {
t.Errorf(
"Marshaled request doesn't match expected: wanted %x, got %x",
expectedBytes,
+1 -1
View File
@@ -104,7 +104,7 @@ func (r *keyring) Unlock(passphrase []byte) error {
if !r.locked {
return errors.New("agent: not locked")
}
if 1 != subtle.ConstantTimeCompare(passphrase, r.passphrase) {
if subtle.ConstantTimeCompare(passphrase, r.passphrase) != 1 {
return fmt.Errorf("agent: incorrect passphrase")
}
+1 -1
View File
@@ -38,7 +38,7 @@ func TestBufferReadwrite(t *testing.T) {
b.write(alphabet[5:15])
r, _ = b.Read(make([]byte, 10))
r2, _ := b.Read(make([]byte, 10))
if r != 10 || r2 != 5 || 15 != r+r2 {
if r != 10 || r2 != 5 || r+r2 != 15 {
t.Fatal("Expected written == read == 15")
}
}
+1 -1
View File
@@ -195,7 +195,7 @@ func TestHandshakeBasic(t *testing.T) {
}
want := []byte{msgRequestSuccess, byte(i)}
if bytes.Compare(p, want) != 0 {
if !bytes.Equal(p, want) {
t.Errorf("message %d: got %v, want %v", i, p, want)
}
}
+1 -1
View File
@@ -1014,7 +1014,7 @@ func TestKnownHostsParsing(t *testing.T) {
input := test.input
if strings.Contains(input, rsaKeyToken) {
expectedKey = rsaPub
input = strings.Replace(test.input, rsaKeyToken, rsaPubSerialized, -1)
input = strings.ReplaceAll(test.input, rsaKeyToken, rsaPubSerialized)
}
marker, hosts, pubKey, comment, rest, err := ParseKnownHosts([]byte(input))
+1 -1
View File
@@ -745,7 +745,7 @@ func TestSessionID(t *testing.T) {
s := <-serverID
c := <-clientID
if bytes.Compare(s, c) != 0 {
if !bytes.Equal(s, c) {
t.Errorf("server session ID (%x) != client session ID (%x)", s, c)
} else if len(s) == 0 {
t.Errorf("client and server SessionID were empty.")