Skip to content

Commit 8cf4dc2

Browse files
committed
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]>
1 parent af45762 commit 8cf4dc2

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

bucket.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,17 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
175175
var value = bucket.write()
176176

177177
// Insert into node.
178-
key = cloneBytes(key)
179-
c.node().put(key, key, value, 0, common.BucketLeafFlag)
178+
// Using new variable here is important: otherwise `key` param
179+
// is marked as leaking and cannot be allocated on stack.
180+
newKey := cloneBytes(key)
181+
c.node().put(newKey, newKey, value, 0, common.BucketLeafFlag)
180182

181183
// Since subbuckets are not allowed on inline buckets, we need to
182184
// dereference the inline page, if it exists. This will cause the bucket
183185
// to be treated as a regular, non-inline bucket for the rest of the tx.
184186
b.page = nil
185187

186-
return b.Bucket(key), nil
188+
return b.Bucket(newKey), nil
187189
}
188190

189191
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
@@ -291,8 +293,10 @@ func (b *Bucket) Put(key []byte, value []byte) error {
291293
}
292294

293295
// Insert into node.
294-
key = cloneBytes(key)
295-
c.node().put(key, key, value, 0, 0)
296+
// Using new variable here is important: otherwise `key` param
297+
// is marked as leaking and cannot be allocated on stack.
298+
newKey := cloneBytes(key)
299+
c.node().put(newKey, newKey, value, 0, 0)
296300

297301
return nil
298302
}

0 commit comments

Comments
 (0)