From 0a521c0b487d04a28e15b3fe4978271310503a6e Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 9 Aug 2023 16:28:14 +0300 Subject: [PATCH] bucket: allow to allocate key on stack in Put() As per `go build -gcflags -m ./... 2>&1`: Old behaviour: ``` ./bucket.go:148:31: leaking param: key ./bucket.go:192:42: leaking param: key ./bucket.go:271:22: leaking param: key ``` Now: ``` ./bucket.go:148:31: key does not escape ./bucket.go:192:42: key does not escape ./bucket.go:271:22: key does not escape ``` Signed-off-by: Evgenii Stratonikov (cherry picked from commit 71a59caf31ca42c3deb8967ceee0a4804d220bf4) Signed-off-by: Wei Fu --- bucket.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bucket.go b/bucket.go index 054467af3..ada05e3b7 100644 --- a/bucket.go +++ b/bucket.go @@ -183,15 +183,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) { var value = bucket.write() // Insert into node. - key = cloneBytes(key) - c.node().put(key, key, value, 0, bucketLeafFlag) + // 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) + c.node().put(newKey, newKey, value, 0, bucketLeafFlag) // Since subbuckets are not allowed on inline buckets, we need to // dereference the inline page, if it exists. This will cause the bucket // to be treated as a regular, non-inline bucket for the rest of the tx. b.page = nil - return b.Bucket(key), nil + return b.Bucket(newKey), nil } // CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it. @@ -298,8 +300,10 @@ func (b *Bucket) Put(key []byte, value []byte) error { } // Insert into node. - key = cloneBytes(key) - c.node().put(key, key, value, 0, 0) + // 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) + c.node().put(newKey, newKey, value, 0, 0) return nil }