Skip to content

Commit f011be0

Browse files
fuweidishan16696
authored andcommitted
*: copy key before comparing during CreateBucket
It's follow-up of etcd-io#637. Signed-off-by: Wei Fu <[email protected]>
1 parent 54979d6 commit f011be0

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

bucket.go

+7-6
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)