Merge pull request #215 from RobbKistler/memleak

Fix mem leak in openssl_auth.cpp:s3fs_sha256hexsum
This commit is contained in:
Takeshi Nakatani 2015-08-12 23:38:51 +09:00
commit 09dff484e1
2 changed files with 8 additions and 4 deletions

View File

@ -263,6 +263,7 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
}else if(-1 == bytes){ }else if(-1 == bytes){
// error // error
DPRNNN("file read error(%d)", errno); DPRNNN("file read error(%d)", errno);
PK11_DestroyContext(sha256ctx, PR_TRUE);
return NULL; return NULL;
} }
PK11_DigestOp(sha256ctx, buf, bytes); PK11_DigestOp(sha256ctx, buf, bytes);

View File

@ -298,9 +298,7 @@ bool s3fs_sha256(const unsigned char* data, unsigned int datalen, unsigned char*
unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size) unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
{ {
const EVP_MD* md = EVP_get_digestbyname("sha256"); const EVP_MD* md = EVP_get_digestbyname("sha256");
EVP_MD_CTX* sha256ctx = EVP_MD_CTX_create(); EVP_MD_CTX* sha256ctx;
EVP_DigestInit_ex(sha256ctx, md, NULL);
char buf[512]; char buf[512];
ssize_t bytes; ssize_t bytes;
unsigned char* result; unsigned char* result;
@ -318,6 +316,9 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
return NULL; return NULL;
} }
sha256ctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(sha256ctx, md, NULL);
memset(buf, 0, 512); memset(buf, 0, 512);
for(ssize_t total = 0; total < size; total += bytes){ for(ssize_t total = 0; total < size; total += bytes){
bytes = 512 < (size - total) ? 512 : (size - total); bytes = 512 < (size - total) ? 512 : (size - total);
@ -328,12 +329,14 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
}else if(-1 == bytes){ }else if(-1 == bytes){
// error // error
DPRNNN("file read error(%d)", errno); DPRNNN("file read error(%d)", errno);
EVP_MD_CTX_destroy(sha256ctx);
return NULL; return NULL;
} }
EVP_DigestUpdate(sha256ctx, buf, bytes); EVP_DigestUpdate(sha256ctx, buf, bytes);
memset(buf, 0, 512); memset(buf, 0, 512);
} }
if(NULL == (result = (unsigned char*)malloc(get_sha256_digest_length()))){ if(NULL == (result = (unsigned char*)malloc(get_sha256_digest_length()))){
EVP_MD_CTX_destroy(sha256ctx);
return NULL; return NULL;
} }
EVP_DigestFinal_ex(sha256ctx, result, NULL); EVP_DigestFinal_ex(sha256ctx, result, NULL);