Skip to content

Commit

Permalink
bucket: allow to allocate key on stack in Put()
Browse files Browse the repository at this point in the history
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 <fyfyrchik@runbox.com>
  • Loading branch information
fyfyrchik committed Aug 10, 2023
1 parent af45762 commit 8cf4dc2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,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, common.BucketLeafFlag)
// Using new variable here is important: otherwise `key` param
// is marked as leaking and cannot be allocated on stack.
newKey := cloneBytes(key)
c.node().put(newKey, newKey, value, 0, common.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.
Expand Down Expand Up @@ -291,8 +293,10 @@ func (b *Bucket) Put(key []byte, value []byte) error {
}

// Insert into node.
key = cloneBytes(key)
c.node().put(key, key, value, 0, 0)
// Using new variable here is important: otherwise `key` param
// is marked as leaking and cannot be allocated on stack.
newKey := cloneBytes(key)
c.node().put(newKey, newKey, value, 0, 0)

return nil
}
Expand Down

0 comments on commit 8cf4dc2

Please sign in to comment.