Skip to content

Commit 62d8026

Browse files
committed
*: copy key before comparing during CreateBucket
It's follow-up of #637. Signed-off-by: Wei Fu <[email protected]>
1 parent c89db63 commit 62d8026

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

bucket.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
154154
return nil, errors.ErrBucketNameRequired
155155
}
156156

157+
// Insert into node.
158+
// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
159+
// it from being marked as leaking, and accordingly cannot be allocated on stack.
160+
newKey := cloneBytes(key)
161+
157162
// Move cursor to correct position.
158163
c := b.Cursor()
159-
k, _, flags := c.seek(key)
164+
k, _, flags := c.seek(newKey)
160165

161166
// Return an error if there is an existing key.
162-
if bytes.Equal(key, k) {
167+
if bytes.Equal(newKey, k) {
163168
if (flags & common.BucketLeafFlag) != 0 {
164169
return nil, errors.ErrBucketExists
165170
}
@@ -174,10 +179,6 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
174179
}
175180
var value = bucket.write()
176181

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

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

0 commit comments

Comments
 (0)