Skip to content

Commit

Permalink
Merge pull request #775 from SiaFoundation/chris/scan-secretKey
Browse files Browse the repository at this point in the history
Return deep copy in secretKey.Scan
  • Loading branch information
ChrisSchinnerl authored Nov 30, 2023
2 parents 15fef6d + 91c6d2a commit c5703ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion stores/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3474,10 +3474,11 @@ func TestDeleteHostSector(t *testing.T) {
}

// create a healthy slab with one sector that is uploaded to all contracts.
key, _ := object.GenerateEncryptionKey().MarshalBinary()
root := types.Hash256{1, 2, 3}
slab := dbSlab{
DBContractSetID: 1,
Key: []byte(object.GenerateEncryptionKey().String()),
Key: key,
Health: 1.0,
HealthValidUntil: time.Now().Add(time.Hour).Unix(),
TotalShards: 1,
Expand Down
20 changes: 13 additions & 7 deletions stores/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"go.sia.tech/core/types"
)

const (
secretKeySize = 32
)

var zeroCurrency = currency(types.ZeroCurrency)

type (
Expand Down Expand Up @@ -47,8 +51,10 @@ func (k *secretKey) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
return errors.New(fmt.Sprint("failed to unmarshal secretKey value:", value))
} else if len(bytes) != secretKeySize {
return fmt.Errorf("failed to unmarshal secretKey value due to invalid number of bytes %v != %v: %v", len(bytes), secretKeySize, value)
}
*k = secretKey(bytes)
*k = append(secretKey{}, secretKey(bytes)...)
return nil
}

Expand All @@ -68,8 +74,8 @@ func (h *hash256) Scan(value interface{}) error {
if !ok {
return errors.New(fmt.Sprint("failed to unmarshal hash256 value:", value))
}
if len(bytes) < len(hash256{}) {
return fmt.Errorf("failed to unmarshal hash256 value due to insufficient bytes %v < %v: %v", len(bytes), len(fileContractID{}), value)
if len(bytes) != len(hash256{}) {
return fmt.Errorf("failed to unmarshal hash256 value due to invalid number of bytes %v != %v: %v", len(bytes), len(fileContractID{}), value)
}
*h = *(*hash256)(bytes)
return nil
Expand All @@ -91,8 +97,8 @@ func (fcid *fileContractID) Scan(value interface{}) error {
if !ok {
return errors.New(fmt.Sprint("failed to unmarshal fcid value:", value))
}
if len(bytes) < len(fileContractID{}) {
return fmt.Errorf("failed to unmarshal fcid value due to insufficient bytes %v < %v: %v", len(bytes), len(fileContractID{}), value)
if len(bytes) != len(fileContractID{}) {
return fmt.Errorf("failed to unmarshal fcid value due to invalid number of bytes %v != %v: %v", len(bytes), len(fileContractID{}), value)
}
*fcid = *(*fileContractID)(bytes)
return nil
Expand Down Expand Up @@ -141,8 +147,8 @@ func (pk *publicKey) Scan(value interface{}) error {
if !ok {
return errors.New(fmt.Sprint("failed to unmarshal publicKey value:", value))
}
if len(bytes) < len(types.PublicKey{}) {
return fmt.Errorf("failed to unmarshal publicKey value due to insufficient bytes %v < %v: %v", len(bytes), len(publicKey{}), value)
if len(bytes) != len(types.PublicKey{}) {
return fmt.Errorf("failed to unmarshal publicKey value due invalid number of bytes %v != %v: %v", len(bytes), len(publicKey{}), value)
}
*pk = *(*publicKey)(bytes)
return nil
Expand Down

0 comments on commit c5703ec

Please sign in to comment.