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

copy key before comparing during CreateBucket or CreateBucketIfNotExists #641

Merged
merged 2 commits into from
Dec 17, 2023
Merged
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
30 changes: 16 additions & 14 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
return nil, errors.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 & common.BucketLeafFlag) != 0 {
return nil, errors.ErrBucketExists
}
Expand All @@ -174,10 +179,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, common.BucketLeafFlag)

// Since subbuckets are not allowed on inline buckets, we need to
Expand All @@ -200,22 +201,27 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
return nil, errors.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)

if b.buckets != nil {
if child := b.buckets[string(key)]; child != nil {
if child := b.buckets[string(newKey)]; child != nil {
return child, nil
}
}

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

// Return an error if there is an existing non-bucket key.
if bytes.Equal(key, k) {
if bytes.Equal(newKey, k) {
if (flags & common.BucketLeafFlag) != 0 {
var child = b.openBucket(v)
if b.buckets != nil {
b.buckets[string(key)] = child
b.buckets[string(newKey)] = child
}

return child, nil
Expand All @@ -231,10 +237,6 @@ func (b *Bucket) CreateBucketIfNotExists(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, common.BucketLeafFlag)

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