Skip to content

Commit

Permalink
*: Use bytes.Clone to copy byte slices
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cthulhu-rider committed Feb 28, 2024
1 parent 8cebe82 commit 729d480
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 76 deletions.
3 changes: 1 addition & 2 deletions client/object_replicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
18 changes: 3 additions & 15 deletions container/container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package container

import (
"bytes"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -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 {
Expand All @@ -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{})
Expand Down
6 changes: 2 additions & 4 deletions eacl/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down
3 changes: 2 additions & 1 deletion object/object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package object

import (
"bytes"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 13 additions & 21 deletions object/object_copy.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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())
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
29 changes: 3 additions & 26 deletions session/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions session/container_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
},
Expand Down
4 changes: 2 additions & 2 deletions session/object_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
},
Expand Down
6 changes: 3 additions & 3 deletions user/id_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 729d480

Please sign in to comment.