Skip to content

Commit

Permalink
Merge pull request #85 from yowcow/fix-boltdb-storage-return-val-prot…
Browse files Browse the repository at this point in the history
…ection

make returning []byte even without RLock
  • Loading branch information
yowcow authored Aug 3, 2018
2 parents 0fde4e5 + 5e014a3 commit 7079126
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions storage/boltstorage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,29 @@ func (s *Storage) Get(key []byte) ([]byte, error) {
}

func getFromBucket(db *bolt.DB, bucket, key []byte) ([]byte, error) {
var val []byte
var retVal []byte

err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return storage.InternalError(fmt.Sprintf("bucket %v not found", bucket))
}
val = b.Get(key)

val := b.Get(key)
if val == nil {
return storage.KeyNotFoundError(key)
}

// Making sure that []byte is safe.
// Without copy, returning []byte may be corrupted at the time of reference later on.
retVal = make([]byte, len(val))
copy(retVal, val)

return nil
})
if err != nil {
return nil, err
}

// Making sure that []byte is safe.
// Without copy, returning []byte may be corrupted at the time of reference later on.
retVal := make([]byte, len(val))
copy(retVal, val)

return retVal, nil
}

0 comments on commit 7079126

Please sign in to comment.