@@ -201,22 +201,27 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
201
201
return nil , errors .ErrBucketNameRequired
202
202
}
203
203
204
+ // Insert into node.
205
+ // Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
206
+ // it from being marked as leaking, and accordingly cannot be allocated on stack.
207
+ newKey := cloneBytes (key )
208
+
204
209
if b .buckets != nil {
205
- if child := b .buckets [string (key )]; child != nil {
210
+ if child := b .buckets [string (newKey )]; child != nil {
206
211
return child , nil
207
212
}
208
213
}
209
214
210
215
// Move cursor to correct position.
211
216
c := b .Cursor ()
212
- k , v , flags := c .seek (key )
217
+ k , v , flags := c .seek (newKey )
213
218
214
219
// Return an error if there is an existing non-bucket key.
215
- if bytes .Equal (key , k ) {
220
+ if bytes .Equal (newKey , k ) {
216
221
if (flags & common .BucketLeafFlag ) != 0 {
217
222
var child = b .openBucket (v )
218
223
if b .buckets != nil {
219
- b .buckets [string (key )] = child
224
+ b .buckets [string (newKey )] = child
220
225
}
221
226
222
227
return child , nil
@@ -232,10 +237,6 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
232
237
}
233
238
var value = bucket .write ()
234
239
235
- // Insert into node.
236
- // Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
237
- // it from being marked as leaking, and accordingly cannot be allocated on stack.
238
- newKey := cloneBytes (key )
239
240
c .node ().put (newKey , newKey , value , 0 , common .BucketLeafFlag )
240
241
241
242
// Since subbuckets are not allowed on inline buckets, we need to
0 commit comments