Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return deep copy in secretKey.Scan #775

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}) {
Copy link
Member

@peterjan peterjan Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit can you change len(fileContractID{}) to len(hash256{}) in the line below this one, maybe also < to !=

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