Skip to content

Commit 7a51062

Browse files
committed
manual: sometimes mangle freed buffers in invariants builds
In invariants builds, sometimes mangle manually-managed buffers just before freeing them. This can help surface code paths that are inadvertently using the memory after it's been freed.
1 parent 6b27dae commit 7a51062

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

internal/manual/manual_cgo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func New(purpose Purpose, n uintptr) Buf {
7575
// returned by New.
7676
func Free(purpose Purpose, b Buf) {
7777
if b.n != 0 {
78+
invariants.MaybeMangle(b.Slice())
7879
recordFree(purpose, b.n)
7980

8081
if !invariants.RaceEnabled || !useGoAllocation {

internal/manual/manual_nocgo.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
package manual
88

9-
import "unsafe"
9+
import (
10+
"unsafe"
11+
12+
"github.com/cockroachdb/pebble/internal/invariants"
13+
)
1014

1115
// Provides versions of New and Free when cgo is not available (e.g. cross
1216
// compilation).
@@ -27,5 +31,6 @@ func New(purpose Purpose, n uintptr) Buf {
2731
// Free frees the specified slice. It has to be exactly the slice that was
2832
// returned by New.
2933
func Free(purpose Purpose, b Buf) {
34+
invariants.MaybeMangle(b.Slice())
3035
recordFree(purpose, b.n)
3136
}

0 commit comments

Comments
 (0)