From 4b958523a13995386797886e93e309fe6f7a80a3 Mon Sep 17 00:00:00 2001 From: kanandev2024 Date: Sun, 17 Dec 2023 21:47:49 +0800 Subject: [PATCH 1/2] *: copy key before comparing during CreateBucket It's follow-up of #637. Signed-off-by: Wei Fu --- bucket.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bucket.go b/bucket.go index 8e1a98a..ba9d319 100644 --- a/bucket.go +++ b/bucket.go @@ -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 } @@ -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 From 74f6d839e7ef3c2ec2f61dead427828c809612f8 Mon Sep 17 00:00:00 2001 From: kanandev2024 Date: Sun, 17 Dec 2023 22:00:18 +0800 Subject: [PATCH 2/2] copy key before seeking in CreateBucketIfNotExists It's follow-up of #637. Signed-off-by: Wei Fu --- bucket.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/bucket.go b/bucket.go index ba9d319..0d60a35 100644 --- a/bucket.go +++ b/bucket.go @@ -201,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 @@ -232,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