@@ -175,15 +175,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
175
175
var value = bucket .write ()
176
176
177
177
// Insert into node.
178
- key = cloneBytes (key )
179
- c .node ().put (key , key , value , 0 , common .BucketLeafFlag )
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 )
181
+ c .node ().put (newKey , newKey , value , 0 , common .BucketLeafFlag )
180
182
181
183
// Since subbuckets are not allowed on inline buckets, we need to
182
184
// dereference the inline page, if it exists. This will cause the bucket
183
185
// to be treated as a regular, non-inline bucket for the rest of the tx.
184
186
b .page = nil
185
187
186
- return b .Bucket (key ), nil
188
+ return b .Bucket (newKey ), nil
187
189
}
188
190
189
191
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
@@ -230,15 +232,17 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
230
232
var value = bucket .write ()
231
233
232
234
// Insert into node.
233
- key = cloneBytes (key )
234
- c .node ().put (key , key , value , 0 , common .BucketLeafFlag )
235
+ // Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
236
+ // it from being marked as leaking, and accordingly cannot be allocated on stack.
237
+ newKey := cloneBytes (key )
238
+ c .node ().put (newKey , newKey , value , 0 , common .BucketLeafFlag )
235
239
236
240
// Since subbuckets are not allowed on inline buckets, we need to
237
241
// dereference the inline page, if it exists. This will cause the bucket
238
242
// to be treated as a regular, non-inline bucket for the rest of the tx.
239
243
b .page = nil
240
244
241
- return b .Bucket (key ), nil
245
+ return b .Bucket (newKey ), nil
242
246
}
243
247
244
248
// DeleteBucket deletes a bucket at the given key.
@@ -333,8 +337,10 @@ func (b *Bucket) Put(key []byte, value []byte) error {
333
337
}
334
338
335
339
// Insert into node.
336
- key = cloneBytes (key )
337
- c .node ().put (key , key , value , 0 , 0 )
340
+ // Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
341
+ // it from being marked as leaking, and accordingly cannot be allocated on stack.
342
+ newKey := cloneBytes (key )
343
+ c .node ().put (newKey , newKey , value , 0 , 0 )
338
344
339
345
return nil
340
346
}
0 commit comments