Skip to content

Commit 0a521c0

Browse files
fyfyrchikfuweid
authored andcommitted
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 <[email protected]> (cherry picked from commit 71a59ca) Signed-off-by: Wei Fu <[email protected]>
1 parent defa564 commit 0a521c0

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

bucket.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
183183
var value = bucket.write()
184184

185185
// Insert into node.
186-
key = cloneBytes(key)
187-
c.node().put(key, key, value, 0, bucketLeafFlag)
186+
// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
187+
// it from being marked as leaking, and accordingly cannot be allocated on stack.
188+
newKey := cloneBytes(key)
189+
c.node().put(newKey, newKey, value, 0, bucketLeafFlag)
188190

189191
// Since subbuckets are not allowed on inline buckets, we need to
190192
// dereference the inline page, if it exists. This will cause the bucket
191193
// to be treated as a regular, non-inline bucket for the rest of the tx.
192194
b.page = nil
193195

194-
return b.Bucket(key), nil
196+
return b.Bucket(newKey), nil
195197
}
196198

197199
// 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 {
298300
}
299301

300302
// Insert into node.
301-
key = cloneBytes(key)
302-
c.node().put(key, key, value, 0, 0)
303+
// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
304+
// it from being marked as leaking, and accordingly cannot be allocated on stack.
305+
newKey := cloneBytes(key)
306+
c.node().put(newKey, newKey, value, 0, 0)
303307

304308
return nil
305309
}

0 commit comments

Comments
 (0)