Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Dec 13, 2024
1 parent 97c0d2c commit 3284129
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"unsafe"
)

var testRSAKey = sync.OnceValue(func() C.GO_EVP_PKEY_PTR {
pkey, err := generateEVPPKey(C.GO_EVP_PKEY_RSA, 512, "")
if err != nil {
var testRSAPkey C.GO_EVP_PKEY_PTR

var initTestRSAKey = sync.OnceFunc(func() {
testRSAPkey, _ = generateEVPPKey(C.GO_EVP_PKEY_RSA, 512, "")
if testRSAPkey == nil {
// Try with a larger key.
pkey, _ = generateEVPPKey(C.GO_EVP_PKEY_RSA, 1024, "")
testRSAPkey, _ = generateEVPPKey(C.GO_EVP_PKEY_RSA, 1024, "")
}
return pkey
})

var cachePKCS1Supported sync.Map
Expand All @@ -38,11 +39,11 @@ func SupportsSignatureRSAPKCS1v15(ch crypto.Hash) (supported bool) {
if ch != 0 && md == nil {
return false
}
pkey := testRSAKey()
if pkey == nil {
initTestRSAKey()
if testRSAPkey == nil {
return false
}
ctx := C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
ctx := C.go_openssl_EVP_PKEY_CTX_new(testRSAPkey, nil)
if ctx == nil {
return false
}
Expand Down

0 comments on commit 3284129

Please sign in to comment.