Skip to content

Commit

Permalink
*: copy key before comparing during CreateBucket
Browse files Browse the repository at this point in the history
It's follow-up of #637.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
(cherry picked from commit 62d8026)
Signed-off-by: Wei Fu <fuweid89@gmail.com>
  • Loading branch information
fuweid committed Dec 18, 2023
1 parent 50ddad0 commit fabe2fb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
return nil, ErrBucketNameRequired
}

// Insert into node.
// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
// it from being marked as leaking, and accordingly cannot be allocated on stack.
newKey := cloneBytes(key)

// Move cursor to correct position.
c := b.Cursor()
k, _, flags := c.seek(key)
k, _, flags := c.seek(newKey)

// Return an error if there is an existing key.
if bytes.Equal(key, k) {
if bytes.Equal(newKey, k) {
if (flags & bucketLeafFlag) != 0 {
return nil, ErrBucketExists
}
Expand All @@ -182,10 +187,6 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
}
var value = bucket.write()

// Insert into node.
// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
// it from being marked as leaking, and accordingly cannot be allocated on stack.
newKey := cloneBytes(key)
c.node().put(newKey, newKey, value, 0, bucketLeafFlag)

// Since subbuckets are not allowed on inline buckets, we need to
Expand Down

0 comments on commit fabe2fb

Please sign in to comment.