Skip to content

Commit

Permalink
prevent RandReader.Read argument from escaping to the heap
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Oct 8, 2024
1 parent c36cc36 commit e5661bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cgo_go124.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build go1.24 && !cmd_go_bootstrap

package openssl

// #cgo noescape go_openssl_RAND_bytes
// #cgo nocallback go_openssl_RAND_bytes
import "C"
19 changes: 19 additions & 0 deletions rand_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package openssl_test

import (
"go/version"
"runtime"
"strings"
"testing"

"github.com/golang-fips/openssl/v2"
Expand All @@ -12,3 +15,19 @@ func TestRand(t *testing.T) {
t.Fatal(err)
}
}

func TestAllocations(t *testing.T) {
n := int(testing.AllocsPerRun(10, func() {
buf := make([]byte, 32)
openssl.RandReader.Read(buf)
sink ^= buf[0]
}))
want := 1
ver := strings.TrimPrefix(runtime.Version(), "devel ")
if version.Compare(ver, "go1.24") >= 0 {
want = 0
}
if n > want {
t.Errorf("allocs = %d, want %d", n, want)
}
}

0 comments on commit e5661bc

Please sign in to comment.