Skip to content

Commit

Permalink
fix memory leak in setupEVP and newCipherCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal authored and dagood committed Mar 20, 2024
1 parent 66bdd79 commit fde4397
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,12 @@ func sliceForAppend(in []byte, n int) (head, tail []byte) {
return
}

func newCipherCtx(kind cipherKind, mode cipherMode, encrypt cipherOp, key, iv []byte) (ctx C.GO_EVP_CIPHER_CTX_PTR, err error) {
func newCipherCtx(kind cipherKind, mode cipherMode, encrypt cipherOp, key, iv []byte) (_ C.GO_EVP_CIPHER_CTX_PTR, err error) {
cipher := loadCipher(kind, mode)
if cipher == nil {
panic("crypto/cipher: unsupported cipher: " + kind.String())
}
ctx = C.go_openssl_EVP_CIPHER_CTX_new()
ctx := C.go_openssl_EVP_CIPHER_CTX_new()
if ctx == nil {
return nil, fail("unable to create EVP cipher ctx")
}
Expand Down
18 changes: 9 additions & 9 deletions evp.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ type verifyFunc func(C.GO_EVP_PKEY_CTX_PTR, *C.uchar, C.size_t, *C.uchar, C.size

func setupEVP(withKey withKeyFunc, padding C.int,
h, mgfHash hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
init initFunc) (ctx C.GO_EVP_PKEY_CTX_PTR, err error) {
init initFunc) (_ C.GO_EVP_PKEY_CTX_PTR, err error) {
var ctx C.GO_EVP_PKEY_CTX_PTR
withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
ctx = C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
return 1
})
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new failed")
}
defer func() {
if err != nil {
if ctx != nil {
Expand All @@ -158,14 +166,6 @@ func setupEVP(withKey withKeyFunc, padding C.int,
}
}
}()

withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
ctx = C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
return 1
})
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new failed")
}
if err := init(ctx); err != nil {
return nil, err
}
Expand Down

0 comments on commit fde4397

Please sign in to comment.