crypto: add nil check for PublicKeys Copy()

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
This commit is contained in:
Ekaterina Pavlova 2024-04-27 14:48:48 +05:30
parent df2a56908b
commit 7c8d2c3ec5
2 changed files with 7 additions and 1 deletions

View file

@ -78,8 +78,12 @@ func (keys PublicKeys) Contains(pKey *PublicKey) bool {
return false 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 { func (keys PublicKeys) Copy() PublicKeys {
if keys == nil {
return nil
}
res := make(PublicKeys, len(keys)) res := make(PublicKeys, len(keys))
copy(res, keys) copy(res, keys)
return res return res

View file

@ -40,6 +40,8 @@ func TestEncodeDecodePublicKey(t *testing.T) {
} }
func TestPublicKeys_Copy(t *testing.T) { func TestPublicKeys_Copy(t *testing.T) {
require.Nil(t, (PublicKeys)(nil).Copy())
pubs := make(PublicKeys, 5) pubs := make(PublicKeys, 5)
for i := range pubs { for i := range pubs {
priv, err := NewPrivateKey() priv, err := NewPrivateKey()