Convert Go string-empty checks to `!= ""`. Way back, `len(x) != 0` was more efficient than `!= ""` and a lot of the Go code in BoringSSL is still based on old habits developed in that era. That's no longer true, so use the (now) more standard form for checking whether a string is non-empty. Change-Id: I2e4698e414ed86e68261e7b4c8446fc2603dd6d7 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/86007 Commit-Queue: Adam Langley <agl@google.com> Reviewed-by: David Benjamin <davidben@google.com>
diff --git a/crypto/cipher/test/nist_cavp/make_cavp.go b/crypto/cipher/test/nist_cavp/make_cavp.go index 1bf23a6..0de782c 100644 --- a/crypto/cipher/test/nist_cavp/make_cavp.go +++ b/crypto/cipher/test/nist_cavp/make_cavp.go
@@ -54,7 +54,7 @@ } if i := strings.IndexRune(s, t.kvDelim); t.kvDelim != 0 && i != -1 { key, value = s[:i], s[i+1:] - if trimmed := strings.TrimSpace(value); len(trimmed) != 0 { + if trimmed := strings.TrimSpace(value); trimmed != "" { value = trimmed } else { value = " " @@ -67,7 +67,7 @@ func (t *Test) translateKeyValue(key, value string) (string, string) { if kv, ok := t.translations[kvPair{key, ""}]; ok { - if len(kv.value) == 0 && len(value) != 0 { + if len(kv.value) == 0 && value != "" { return kv.key, value } return kv.key, kv.value @@ -98,7 +98,7 @@ // Auxiliary labels passed as a flag. cmdLineLabels := make(map[string]string) - if len(cmdLineLabelStr) != 0 { + if cmdLineLabelStr != "" { pairs := strings.Split(cmdLineLabelStr, ",") for _, p := range pairs { key, value := t.parseKeyValue(p) @@ -149,7 +149,7 @@ k, v := t.parseKeyValue(l[1 : len(l)-1]) k, v = t.translateKeyValue(k, v) - if len(k) != 0 { + if k != "" { labels[k] = v } @@ -182,7 +182,7 @@ if *cipher == "tdes" && k == "Key" { v += v + v // Key1=Key2=Key3 } - if len(k) != 0 { + if k != "" { printKeyValue(k, v) currentKv[k] = v }
diff --git a/crypto/fipsmodule/bn/bn_test_to_fuzzer.go b/crypto/fipsmodule/bn/bn_test_to_fuzzer.go index b196424..6e100cc 100644 --- a/crypto/fipsmodule/bn/bn_test_to_fuzzer.go +++ b/crypto/fipsmodule/bn/bn_test_to_fuzzer.go
@@ -220,7 +220,7 @@ } } - if len(fuzzer) != 0 { + if fuzzer != "" { hash := sha1.Sum(b) path := filepath.Join(fuzzerDir, fuzzer+"_corpus", hex.EncodeToString(hash[:])) if err := os.WriteFile(path, b, 0666); err != nil {
diff --git a/crypto/obj/objects.go b/crypto/obj/objects.go index eeff069..8ebbd5b 100644 --- a/crypto/obj/objects.go +++ b/crypto/obj/objects.go
@@ -254,7 +254,7 @@ case "!Alias": // !Alias directives define an alias for an OID // without emitting an object. - if len(nextName) != 0 { + if nextName != "" { return nil, withLine(errors.New("!Cname directives may not modify !Alias directives.")) } if len(args) < 3 {
diff --git a/ssl/test/runner/runner.go b/ssl/test/runner/runner.go index 57f9cc4..892ec5d 100644 --- a/ssl/test/runner/runner.go +++ b/ssl/test/runner/runner.go
@@ -725,7 +725,7 @@ if *flagDebug { defer connDebug.WriteTo(os.Stdout) } - if len(*transcriptDir) != 0 { + if *transcriptDir != "" { defer func() { if num == len(*transcripts) { *transcripts = append(*transcripts, connDebug.Transcript()) @@ -1445,10 +1445,10 @@ } } - if len(cred.ChainPath) != 0 { + if cred.ChainPath != "" { flags = append(flags, prefix+"-cert-file", cred.ChainPath) } - if len(cred.KeyPath) != 0 { + if cred.KeyPath != "" { flags = append(flags, prefix+"-key-file", cred.KeyPath) } handleBase64Field("ocsp-response", cred.OCSPStaple) @@ -1671,7 +1671,7 @@ var transcriptPrefix string var transcripts [][]byte - if len(*transcriptDir) != 0 { + if *transcriptDir != "" { protocol := "tls" if test.protocol == dtls { protocol = "dtls" @@ -1775,7 +1775,7 @@ if localErr != nil { localErrString = localErr.Error() } - if len(test.expectedLocalError) != 0 { + if test.expectedLocalError != "" { correctFailure = correctFailure && strings.Contains(localErrString, test.expectedLocalError) } @@ -2181,13 +2181,12 @@ initKeys() initCertificates() - if len(*shimConfigFile) != 0 { + if *shimConfigFile != "" { encoded, err := os.ReadFile(*shimConfigFile) if err != nil { fmt.Fprintf(os.Stderr, "Couldn't read config file %q: %s\n", *shimConfigFile, err) os.Exit(1) } - if err := json.Unmarshal(encoded, &shimConfig); err != nil { fmt.Fprintf(os.Stderr, "Couldn't decode config file %q: %s\n", *shimConfigFile, err) os.Exit(1)
diff --git a/util/convert_wycheproof/convert_wycheproof.go b/util/convert_wycheproof/convert_wycheproof.go index 8018738..6125275 100644 --- a/util/convert_wycheproof/convert_wycheproof.go +++ b/util/convert_wycheproof/convert_wycheproof.go
@@ -159,7 +159,7 @@ if _, err := fmt.Fprintf(f, "# tcId = %d\n", int(test["tcId"].(float64))); err != nil { return err } - if comment, ok := test["comment"]; ok && len(comment.(string)) != 0 { + if comment, ok := test["comment"]; ok && comment.(string) != "" { if err := printComment(f, comment.(string)); err != nil { return err }
diff --git a/util/fipstools/acvp/acvptool/acvp.go b/util/fipstools/acvp/acvptool/acvp.go index 7a5c43c..4082c3f 100644 --- a/util/fipstools/acvp/acvptool/acvp.go +++ b/util/fipstools/acvp/acvptool/acvp.go
@@ -394,7 +394,7 @@ if len(config.PrivateKeyDERFile) == 0 && len(config.PrivateKeyFile) == 0 { return nil, errors.New("config file missing PrivateKeyDERFile and PrivateKeyFile") } - if len(config.PrivateKeyDERFile) != 0 && len(config.PrivateKeyFile) != 0 { + if config.PrivateKeyDERFile != "" && config.PrivateKeyFile != "" { return nil, errors.New("config file has both PrivateKeyDERFile and PrivateKeyFile - can only have one") } privateKeyFile := config.PrivateKeyDERFile
diff --git a/util/fipstools/acvp/acvptool/acvp/acvp.go b/util/fipstools/acvp/acvptool/acvp/acvp.go index feb4220..3a3ab39 100644 --- a/util/fipstools/acvp/acvptool/acvp/acvp.go +++ b/util/fipstools/acvp/acvptool/acvp/acvp.go
@@ -373,7 +373,7 @@ if err != nil { return nil, err } - if len(token) != 0 && endpoint != loginEndpoint { + if token != "" && endpoint != loginEndpoint { req.Header.Add("Authorization", "Bearer "+token) } return req, nil @@ -514,7 +514,7 @@ if err != nil { return err } - if len(token) != 0 { + if token != "" { req.Header.Add("Authorization", "Bearer "+token) } resp, err := server.client.Do(req)
diff --git a/util/fipstools/delocate/delocate.go b/util/fipstools/delocate/delocate.go index 6870a24..fd8b25b 100644 --- a/util/fipstools/delocate/delocate.go +++ b/util/fipstools/delocate/delocate.go
@@ -454,7 +454,7 @@ return statement, nil } - if len(offsetStr) != 0 { + if offsetStr != "" { panic("non-zero offset for helper-based reference") } @@ -536,7 +536,7 @@ case ruleMemoryRef: assertNodeType(argNodes[1].up, ruleSymbolRef) node, empty := d.gatherOffsets(argNodes[1].up.up, "") - if len(empty) != 0 { + if empty != "" { panic("prefix offsets found for adrp") } symbol = d.contents(node) @@ -1277,7 +1277,7 @@ if changed { d.writeCommentedNode(statement) replacement := "\t" + instructionName + "\t" + strings.Join(args, ", ") + "\n" - if len(prefix) != 0 { + if prefix != "" { replacement = "\t" + prefix + replacement } wrappers.do(func() {
diff --git a/util/godeps.go b/util/godeps.go index 5fc9c85..f653125 100644 --- a/util/godeps.go +++ b/util/godeps.go
@@ -182,7 +182,7 @@ sort.Strings(files) outFile := os.Stdout - if len(*out) != 0 { + if *out != "" { var err error outFile, err = os.Create(*out) if err != nil {
diff --git a/util/run_android_tests.go b/util/run_android_tests.go index 3ef22b8..a0571a5 100644 --- a/util/run_android_tests.go +++ b/util/run_android_tests.go
@@ -179,7 +179,7 @@ } func detectOptionsFromCMake() error { - if len(*ndkPath) != 0 && len(*abi) != 0 && *apiLevel != 0 { + if *ndkPath != "" && *abi != "" && *apiLevel != 0 { // No need to parse options from CMake. return nil }