From d386e5b28dc603ac4ffac10337f322e15bc92d80 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 23 Mar 2023 12:59:40 +0300 Subject: [PATCH] Use bytes.Equal() instead of bytes.Compare()==0 --- src/cfg.go | 6 +++--- src/check.go | 4 ++-- src/cmd/nncp-cfgenc/main.go | 2 +- src/cmd/nncp-reass/main.go | 2 +- src/mth_test.go | 18 +++++++----------- src/pkt.go | 2 +- src/pkt_test.go | 2 +- src/sp.go | 2 +- src/toss_test.go | 9 ++++----- src/tx_test.go | 4 ++-- 10 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/cfg.go b/src/cfg.go index 52bd3f3..a57277e 100644 --- a/src/cfg.go +++ b/src/cfg.go @@ -468,7 +468,7 @@ func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) { func CfgParse(data []byte) (*CfgJSON, error) { var err error - if bytes.Compare(data[:8], MagicNNCPBv3.B[:]) == 0 { + if bytes.Equal(data[:8], MagicNNCPBv3.B[:]) { os.Stderr.WriteString("Passphrase:") password, err := term.ReadPassword(0) if err != nil { @@ -479,9 +479,9 @@ func CfgParse(data []byte) (*CfgJSON, error) { if err != nil { return nil, err } - } else if bytes.Compare(data[:8], MagicNNCPBv2.B[:]) == 0 { + } else if bytes.Equal(data[:8], MagicNNCPBv2.B[:]) { log.Fatalln(MagicNNCPBv2.TooOld()) - } else if bytes.Compare(data[:8], MagicNNCPBv1.B[:]) == 0 { + } else if bytes.Equal(data[:8], MagicNNCPBv1.B[:]) { log.Fatalln(MagicNNCPBv1.TooOld()) } var cfgGeneral map[string]interface{} diff --git a/src/check.go b/src/check.go index 6a1b18a..a4e0d06 100644 --- a/src/check.go +++ b/src/check.go @@ -44,7 +44,7 @@ func Check( ); err != nil { return false, err } - return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil + return bytes.Equal(hsh.Sum(nil), checksum), nil } func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool { @@ -113,7 +113,7 @@ func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[MTHSize]byte, mth MTH) (int ); err != nil { return 0, err } - if bytes.Compare(mth.Sum(nil), hshValue[:]) == 0 { + if bytes.Equal(mth.Sum(nil), hshValue[:]) { gut = true } } diff --git a/src/cmd/nncp-cfgenc/main.go b/src/cmd/nncp-cfgenc/main.go index ef4a430..5fc83e2 100644 --- a/src/cmd/nncp-cfgenc/main.go +++ b/src/cmd/nncp-cfgenc/main.go @@ -115,7 +115,7 @@ func main() { log.Fatalln(err) } os.Stderr.WriteString("\n") - if bytes.Compare(password1, password2) != 0 { + if !bytes.Equal(password1, password2) { log.Fatalln(errors.New("Passphrases do not match")) } eblob, err := nncp.NewEBlob(*sOpt, *tOpt, *pOpt, password1, data) diff --git a/src/cmd/nncp-reass/main.go b/src/cmd/nncp-reass/main.go index fc54a5b..3961449 100644 --- a/src/cmd/nncp-reass/main.go +++ b/src/cmd/nncp-reass/main.go @@ -164,7 +164,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo log.Fatalln(err) } fd.Close() - if bytes.Compare(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) != 0 { + if !bytes.Equal(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) { ctx.LogE( "reass-chunk", nncp.LEs{{K: "Path", V: path}, {K: "Chunk", V: chunkNum}}, diff --git a/src/mth_test.go b/src/mth_test.go index 6d6b5c3..fcdff17 100644 --- a/src/mth_test.go +++ b/src/mth_test.go @@ -49,13 +49,13 @@ func TestMTHSeqSymmetric(t *testing.T) { if _, err := mth.PreaddFrom(bytes.NewReader(data), "", false); err != nil { panic(err) } - if bytes.Compare(hsh0, mth.Sum(nil)) != 0 { + if !bytes.Equal(hsh0, mth.Sum(nil)) { return false } mth = MTHSeqNew(0, 0) mth.Write(data) - if bytes.Compare(hsh0, mth.Sum(nil)) != 0 { + if !bytes.Equal(hsh0, mth.Sum(nil)) { return false } @@ -65,7 +65,7 @@ func TestMTHSeqSymmetric(t *testing.T) { panic(err) } hsh00 := mth.Sum(nil) - if bytes.Compare(hsh0, hsh00) == 0 { + if bytes.Equal(hsh0, hsh00) { return false } @@ -76,17 +76,13 @@ func TestMTHSeqSymmetric(t *testing.T) { if _, err := mth.PreaddFrom(bytes.NewReader(data), "", false); err != nil { panic(err) } - if bytes.Compare(hsh00, mth.Sum(nil)) != 0 { + if !bytes.Equal(hsh00, mth.Sum(nil)) { return false } mth = MTHSeqNew(0, 0) mth.Write(data) - if bytes.Compare(hsh00, mth.Sum(nil)) != 0 { - return false - } - - return true + return bytes.Equal(hsh00, mth.Sum(nil)) } if err := quick.Check(f, nil); err != nil { t.Error(err) @@ -110,7 +106,7 @@ func TestMTHSeqAndFatEqual(t *testing.T) { if _, err := io.Copy(seq, bytes.NewReader(data)); err != nil { panic(err) } - return bytes.Compare(hshFat, seq.Sum(nil)) == 0 + return bytes.Equal(hshFat, seq.Sum(nil)) } if err := quick.Check(f, nil); err != nil { t.Error(err) @@ -128,7 +124,7 @@ func TestMTHNull(t *testing.T) { if _, err := seq.Write(nil); err != nil { t.Error(err) } - if bytes.Compare(hshFat, seq.Sum(nil)) != 0 { + if !bytes.Equal(hshFat, seq.Sum(nil)) { t.FailNow() } } diff --git a/src/pkt.go b/src/pkt.go index f6ab749..38e4659 100644 --- a/src/pkt.go +++ b/src/pkt.go @@ -514,7 +514,7 @@ FullRead: if err != nil { panic(err) } - if bytes.Compare(ct[:n], pt[:n]) != 0 { + if !bytes.Equal(ct[:n], pt[:n]) { err = errors.New("wrong pad value") return } diff --git a/src/pkt_test.go b/src/pkt_test.go index 5077c57..d90877d 100644 --- a/src/pkt_test.go +++ b/src/pkt_test.go @@ -146,7 +146,7 @@ func TestPktEncRead(t *testing.T) { } var pktBuf bytes.Buffer xdr.Marshal(&pktBuf, &pkt) - return bytes.Compare(pt.Bytes(), append(pktBuf.Bytes(), data...)) == 0 + return bytes.Equal(pt.Bytes(), append(pktBuf.Bytes(), data...)) } if err := quick.Check(f, nil); err != nil { t.Error(err) diff --git a/src/sp.go b/src/sp.go index 1077563..29fbf9b 100644 --- a/src/sp.go +++ b/src/sp.go @@ -1477,7 +1477,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { if hasherAndOffset != nil { delete(state.fileHashers, filePath) if hasherAndOffset.mth.PreaddSize() == 0 { - if bytes.Compare(hasherAndOffset.mth.Sum(nil), file.Hash[:]) != 0 { + if !bytes.Equal(hasherAndOffset.mth.Sum(nil), file.Hash[:]) { state.Ctx.LogE( "sp-file-bad-checksum", lesp, errors.New("checksum mismatch"), diff --git a/src/toss_test.go b/src/toss_test.go index e0f682b..b17fc7b 100644 --- a/src/toss_test.go +++ b/src/toss_test.go @@ -150,7 +150,7 @@ func TestTossExec(t *testing.T) { ) expected = append(expected, []byte("BODY\n")...) } - return bytes.Compare(mbox, expected) == 0 + return bytes.Equal(mbox, expected) } if err := quick.Check(f, nil); err != nil { t.Error(err) @@ -240,7 +240,7 @@ func TestTossFile(t *testing.T) { if err != nil { panic(err) } - if bytes.Compare(data, fileData) != 0 { + if !bytes.Equal(data, fileData) { return false } } @@ -423,8 +423,7 @@ func TestTossFreq(t *testing.T) { if pkt.Nice != replyNice { return false } - dst := string(pkt.Path[:int(pkt.PathLen)]) - if bytes.Compare(buf.Bytes(), files[dst]) != 0 { + if !bytes.Equal(buf.Bytes(), files[string(pkt.Path[:int(pkt.PathLen)])]) { return false } } @@ -514,7 +513,7 @@ func TestTossTrns(t *testing.T) { panic(err) } for k, data := range datum { - if bytes.Compare(dataRead, data) == 0 { + if bytes.Equal(dataRead, data) { delete(datum, k) } } diff --git a/src/tx_test.go b/src/tx_test.go index 9c061ee..6c0aa78 100644 --- a/src/tx_test.go +++ b/src/tx_test.go @@ -148,14 +148,14 @@ func TestTx(t *testing.T) { if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) { return false } - if bytes.Compare(bufR.Bytes(), []byte(data)) != 0 { + if !bytes.Equal(bufR.Bytes(), []byte(data)) { return false } } else { if pkt.Type != PktTypeTrns { return false } - if bytes.Compare(pkt.Path[:MTHSize], vias[i+1][:]) != 0 { + if !bytes.Equal(pkt.Path[:MTHSize], vias[i+1][:]) { return false } } -- 2.44.0