diff --git a/bucket.go b/bucket.go index 9fadae1d7..8b93824d7 100644 --- a/bucket.go +++ b/bucket.go @@ -288,9 +288,14 @@ func (b *Bucket) Put(key []byte, value []byte) error { return ErrValueTooLarge } + // 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 with a bucket value. if bytes.Equal(key, k) && (flags&bucketLeafFlag) != 0 { @@ -299,9 +304,7 @@ func (b *Bucket) Put(key []byte, value []byte) error { // gofail: var beforeBucketPut struct{} - // Insert into node. - key = cloneBytes(key) - c.node().put(key, key, value, 0, 0) + c.node().put(newKey, newKey, value, 0, 0) return nil }