From 729d4801a219de63ab7f8d66ec8feca587f0f511 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 28 Feb 2024 10:19:08 +0400 Subject: [PATCH] *: Use `bytes.Clone` to copy byte slices New stdlib function was introduced in Go 1.20. It completely replaces the previously used `slice.Copy` utility. Manual `make`+`copy` no longer needed too. Signed-off-by: Leonard Lyubich --- client/object_replicate_test.go | 3 +-- container/container.go | 18 +++-------------- eacl/target.go | 6 ++---- object/object.go | 3 ++- object/object_copy.go | 34 +++++++++++++-------------------- session/common.go | 29 +++------------------------- session/container_test.go | 4 ++-- session/object_test.go | 4 ++-- user/id_test.go | 6 +++--- 9 files changed, 31 insertions(+), 76 deletions(-) diff --git a/client/object_replicate_test.go b/client/object_replicate_test.go index bc0a0424..44340808 100644 --- a/client/object_replicate_test.go +++ b/client/object_replicate_test.go @@ -9,7 +9,6 @@ import ( "sync" "testing" - "github.com/nspcc-dev/neo-go/pkg/util/slice" objectgrpc "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" status "github.com/nspcc-dev/neofs-api-go/v2/status/grpc" @@ -188,7 +187,7 @@ func TestClient_ReplicateObject(t *testing.T) { err := cli.ReplicateObject(ctx, demuxObj, signer) require.NoError(t, err) - msgCp := slice.Copy(demuxObj.(*demuxReplicationMessage).msg) + msgCp := bytes.Clone(demuxObj.(*demuxReplicationMessage).msg) initBufPtr := &demuxObj.(*demuxReplicationMessage).msg[0] var wg sync.WaitGroup diff --git a/container/container.go b/container/container.go index 03a5f34e..479fd47e 100644 --- a/container/container.go +++ b/container/container.go @@ -1,6 +1,7 @@ package container import ( + "bytes" "errors" "fmt" "strconv" @@ -51,14 +52,7 @@ func (x Container) CopyTo(dst *Container) { if owner := x.v2.GetOwnerID(); owner != nil { var newOwner refs.OwnerID - if val := owner.GetValue(); val != nil { - bts := make([]byte, len(val)) - copy(bts, val) - - newOwner.SetValue(bts) - } else { - newOwner.SetValue(nil) - } + newOwner.SetValue(bytes.Clone(owner.GetValue())) dst.v2.SetOwnerID(&newOwner) } else { @@ -74,13 +68,7 @@ func (x Container) CopyTo(dst *Container) { } // do we need to set the different nonce? - if nonce := x.v2.GetNonce(); nonce != nil { - newNonce := make([]byte, len(nonce)) - copy(newNonce, nonce) - dst.v2.SetNonce(nonce) - } else { - dst.v2.SetNonce(nil) - } + dst.v2.SetNonce(bytes.Clone(x.v2.GetNonce())) if len(x.v2.GetAttributes()) > 0 { dst.v2.SetAttributes([]container.Attribute{}) diff --git a/eacl/target.go b/eacl/target.go index 32f7a80a..bbe65039 100644 --- a/eacl/target.go +++ b/eacl/target.go @@ -32,10 +32,8 @@ func (t Target) CopyTo(dst *Target) { dst.role = t.role dst.keys = make([][]byte, len(t.keys)) - for i, k := range t.keys { - dst.keys[i] = make([]byte, len(k)) - - copy(dst.keys[i], t.keys[i]) + for i := range t.keys { + dst.keys[i] = bytes.Clone(t.keys[i]) } } diff --git a/object/object.go b/object/object.go index 87271ee4..ca5b03a9 100644 --- a/object/object.go +++ b/object/object.go @@ -1,6 +1,7 @@ package object import ( + "bytes" "errors" "fmt" "strings" @@ -73,7 +74,7 @@ func (o Object) CopyTo(dst *Object) { header := (*object.Object)(&o).GetHeader() (*object.Object)(dst).SetHeader(copyHeader(header)) - dst.SetPayload(copyByteSlice(o.Payload())) + dst.SetPayload(bytes.Clone(o.Payload())) } // MarshalHeaderJSON marshals object's header into JSON format. diff --git a/object/object_copy.go b/object/object_copy.go index 9fe716ca..9771a476 100644 --- a/object/object_copy.go +++ b/object/object_copy.go @@ -1,28 +1,20 @@ package object import ( + "bytes" + "github.com/nspcc-dev/neofs-api-go/v2/object" "github.com/nspcc-dev/neofs-api-go/v2/refs" v2session "github.com/nspcc-dev/neofs-api-go/v2/session" ) -func copyByteSlice(sl []byte) []byte { - if sl == nil { - return nil - } - - bts := make([]byte, len(sl)) - copy(bts, sl) - return bts -} - func copyObjectID(id *refs.ObjectID) *refs.ObjectID { if id == nil { return nil } var newID refs.ObjectID - newID.SetValue(copyByteSlice(id.GetValue())) + newID.SetValue(bytes.Clone(id.GetValue())) return &newID } @@ -34,8 +26,8 @@ func copySignature(sig *refs.Signature) *refs.Signature { var newSig refs.Signature newSig.SetScheme(sig.GetScheme()) - newSig.SetKey(copyByteSlice(sig.GetKey())) - newSig.SetSign(copyByteSlice(sig.GetSign())) + newSig.SetKey(bytes.Clone(sig.GetKey())) + newSig.SetSign(bytes.Clone(sig.GetSign())) return &newSig } @@ -48,11 +40,11 @@ func copySession(session *v2session.Token) *v2session.Token { var newSession v2session.Token if body := session.GetBody(); body != nil { var newBody v2session.TokenBody - newBody.SetID(copyByteSlice(body.GetID())) + newBody.SetID(bytes.Clone(body.GetID())) if ownerID := body.GetOwnerID(); ownerID != nil { var newOwnerID refs.OwnerID - newOwnerID.SetValue(copyByteSlice(ownerID.GetValue())) + newOwnerID.SetValue(bytes.Clone(ownerID.GetValue())) newBody.SetOwnerID(&newOwnerID) } else { @@ -66,7 +58,7 @@ func copySession(session *v2session.Token) *v2session.Token { newBody.SetLifetime(nil) } - newBody.SetSessionKey(copyByteSlice(body.GetSessionKey())) + newBody.SetSessionKey(bytes.Clone(body.GetSessionKey())) // it is an interface. Both implementations do nothing inside implemented functions. newBody.SetContext(body.GetContext()) @@ -103,7 +95,7 @@ func copySplitHeader(spl *object.SplitHeader) *object.SplitHeader { newSpl.SetChildren(nil) } - newSpl.SetSplitID(copyByteSlice(spl.GetSplitID())) + newSpl.SetSplitID(bytes.Clone(spl.GetSplitID())) return &newSpl } @@ -128,7 +120,7 @@ func copyHeader(header *object.Header) *object.Header { if containerID := header.GetContainerID(); containerID != nil { var newContainerID refs.ContainerID - newContainerID.SetValue(copyByteSlice(containerID.GetValue())) + newContainerID.SetValue(bytes.Clone(containerID.GetValue())) newHeader.SetContainerID(&newContainerID) } else { @@ -137,7 +129,7 @@ func copyHeader(header *object.Header) *object.Header { if ownerID := header.GetOwnerID(); ownerID != nil { var newOwnerID refs.OwnerID - newOwnerID.SetValue(copyByteSlice(ownerID.GetValue())) + newOwnerID.SetValue(bytes.Clone(ownerID.GetValue())) newHeader.SetOwnerID(&newOwnerID) } else { @@ -147,7 +139,7 @@ func copyHeader(header *object.Header) *object.Header { if payloadHash := header.GetPayloadHash(); payloadHash != nil { var newPayloadHash refs.Checksum newPayloadHash.SetType(payloadHash.GetType()) - newPayloadHash.SetSum(copyByteSlice(payloadHash.GetSum())) + newPayloadHash.SetSum(bytes.Clone(payloadHash.GetSum())) newHeader.SetPayloadHash(&newPayloadHash) } else { @@ -157,7 +149,7 @@ func copyHeader(header *object.Header) *object.Header { if homoHash := header.GetHomomorphicHash(); homoHash != nil { var newHomoHash refs.Checksum newHomoHash.SetType(homoHash.GetType()) - newHomoHash.SetSum(copyByteSlice(homoHash.GetSum())) + newHomoHash.SetSum(bytes.Clone(homoHash.GetSum())) newHeader.SetHomomorphicHash(&newHomoHash) } else { diff --git a/session/common.go b/session/common.go index 7636c67e..9c7f525f 100644 --- a/session/common.go +++ b/session/common.go @@ -42,34 +42,11 @@ func (x commonData) copyTo(dst *commonData) { dst.iat = x.iat dst.nbf = x.nbf dst.exp = x.exp - - if auth := x.authKey; auth != nil { - dst.authKey = make([]byte, len(x.authKey)) - copy(dst.authKey, x.authKey) - } else { - dst.authKey = nil - } - + dst.authKey = bytes.Clone(x.authKey) dst.sigSet = x.sigSet - if sig := x.sig.GetKey(); sig != nil { - bts := make([]byte, len(sig)) - copy(bts, sig) - - dst.sig.SetKey(bts) - } else { - dst.sig.SetKey(nil) - } - + dst.sig.SetKey(bytes.Clone(x.sig.GetKey())) dst.sig.SetScheme(x.sig.GetScheme()) - - if sign := x.sig.GetSign(); sign != nil { - bts := make([]byte, len(sign)) - copy(bts, sign) - - dst.sig.SetSign(sign) - } else { - dst.sig.SetSign(nil) - } + dst.sig.SetSign(bytes.Clone(x.sig.GetSign())) } // reads commonData and custom context from the session.Token message. diff --git a/session/container_test.go b/session/container_test.go index d81f15a1..ad761778 100644 --- a/session/container_test.go +++ b/session/container_test.go @@ -1,13 +1,13 @@ package session_test import ( + "bytes" "fmt" "math" "math/rand" "testing" "github.com/google/uuid" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neofs-api-go/v2/refs" v2session "github.com/nspcc-dev/neofs-api-go/v2/session" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" @@ -177,7 +177,7 @@ func TestContainerProtocolV2(t *testing.T) { breakSign: func(m *v2session.Token) { body := m.GetBody() key := body.GetSessionKey() - cp := slice.Copy(key) + cp := bytes.Clone(key) cp[len(cp)-1]++ body.SetSessionKey(cp) }, diff --git a/session/object_test.go b/session/object_test.go index 40344511..9f70c320 100644 --- a/session/object_test.go +++ b/session/object_test.go @@ -1,13 +1,13 @@ package session_test import ( + "bytes" "fmt" "math" "math/rand" "testing" "github.com/google/uuid" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neofs-api-go/v2/refs" v2session "github.com/nspcc-dev/neofs-api-go/v2/session" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" @@ -182,7 +182,7 @@ func TestObjectProtocolV2(t *testing.T) { breakSign: func(m *v2session.Token) { body := m.GetBody() key := body.GetSessionKey() - cp := slice.Copy(key) + cp := bytes.Clone(key) cp[len(cp)-1]++ body.SetSessionKey(cp) }, diff --git a/user/id_test.go b/user/id_test.go index 49555020..97db311c 100644 --- a/user/id_test.go +++ b/user/id_test.go @@ -1,12 +1,12 @@ package user_test import ( + "bytes" "math/rand" "testing" "github.com/mr-tron/base58" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-sdk-go/user" usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" @@ -72,7 +72,7 @@ func TestV2_ID(t *testing.T) { }) t.Run("invalid prefix", func(t *testing.T) { - val := slice.Copy(val) + val := bytes.Clone(val) val[0]++ m.SetValue(val) @@ -82,7 +82,7 @@ func TestV2_ID(t *testing.T) { }) t.Run("invalid checksum", func(t *testing.T) { - val := slice.Copy(val) + val := bytes.Clone(val) val[21]++ m.SetValue(val)