Skip to content

Commit

Permalink
crypto: add nil check for PublicKeys Copy()
Browse files Browse the repository at this point in the history
Signed-off-by: Ekaterina Pavlova <[email protected]>
  • Loading branch information
AliceInHunterland committed Apr 27, 2024
1 parent 8f9c819 commit f525e0c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/crypto/keys/publickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ func (keys PublicKeys) Contains(pKey *PublicKey) bool {
return false
}

// Copy returns a copy of keys.
// Copy returns a shallow copy of the PublicKeys slice. It creates a new slice with the same elements,
// but does not perform a deep copy of the elements themselves.
func (keys PublicKeys) Copy() PublicKeys {
if keys == nil {
return nil
}
res := make(PublicKeys, len(keys))
copy(res, keys)
return res
Expand Down
2 changes: 2 additions & 0 deletions pkg/crypto/keys/publickey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func TestEncodeDecodePublicKey(t *testing.T) {
}

func TestPublicKeys_Copy(t *testing.T) {
require.Nil(t, (PublicKeys)(nil).Copy())

pubs := make(PublicKeys, 5)
for i := range pubs {
priv, err := NewPrivateKey()
Expand Down

0 comments on commit f525e0c

Please sign in to comment.