Skip to content

Commit

Permalink
Fix some memory leaks, see #79
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed May 6, 2020
1 parent 1bb2df0 commit a89dd54
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SEXP R_parse_der_pubkey(SEXP input){
bail(!!pkey);
unsigned char *buf = NULL;
int len = i2d_PUBKEY(pkey, &buf);
EVP_PKEY_free(pkey);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand All @@ -26,6 +27,7 @@ SEXP R_parse_der_key(SEXP input){
bail(!!pkey);
unsigned char *buf = NULL;
int len = i2d_PrivateKey(pkey, &buf);
EVP_PKEY_free(pkey);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand All @@ -39,6 +41,7 @@ SEXP R_parse_der_cert(SEXP input){
bail(!!cert);
unsigned char *buf = NULL;
int len = i2d_X509(cert, &buf);
X509_free(cert);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand All @@ -54,6 +57,7 @@ SEXP R_derive_pubkey(SEXP input){
bail(!!pkey);
unsigned char *buf = NULL;
int len = i2d_PUBKEY(pkey, &buf);
EVP_PKEY_free(pkey);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand All @@ -67,9 +71,11 @@ SEXP R_cert_pubkey(SEXP input){
X509 *cert = d2i_X509(NULL, &ptr, LENGTH(input));
bail(!!cert);
EVP_PKEY *key = X509_get_pubkey(cert);
X509_free(cert);
bail(!!key);
unsigned char *buf = NULL;
int len = i2d_PUBKEY(key, &buf);
EVP_PKEY_free(key);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand Down
4 changes: 4 additions & 0 deletions src/pem.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SEXP R_parse_pem_key(SEXP input, SEXP password){
bail(!!pkey);
unsigned char *buf = NULL;
int len = i2d_PrivateKey(pkey, &buf);
EVP_PKEY_free(pkey);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand All @@ -57,6 +58,7 @@ SEXP R_parse_pem_pubkey(SEXP input){
bail(!!pkey);
unsigned char *buf = NULL;
int len = i2d_PUBKEY(pkey, &buf);
EVP_PKEY_free(pkey);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand All @@ -69,6 +71,7 @@ SEXP R_parse_pem_cert(SEXP input){
X509 *cert = PEM_read_bio_X509(mem, NULL, password_cb, NULL);
unsigned char *buf = NULL;
int len = i2d_X509(cert, &buf);
X509_free(cert);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand All @@ -83,6 +86,7 @@ SEXP R_parse_pem_pubkey_pkcs1(SEXP input){
bail(!!rsa);
unsigned char *buf = NULL;
int len = i2d_RSA_PUBKEY(rsa, &buf);
RSA_free(rsa);
bail(len);
SEXP res = allocVector(RAWSXP, len);
memcpy(RAW(res), buf, len);
Expand Down

0 comments on commit a89dd54

Please sign in to comment.