Merge pull request #999 from gaul/malloc

Prefer new over malloc
This commit is contained in:
Takeshi Nakatani 2019-04-07 20:21:58 +09:00 committed by GitHub
commit d7bb834bcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 76 additions and 156 deletions

View File

@ -147,9 +147,7 @@ typedef struct xattr_value{
explicit xattr_value(unsigned char* pval = NULL, size_t len = 0) : pvalue(pval), length(len) {}
~xattr_value()
{
if(pvalue){
free(pvalue);
}
delete[] pvalue;
}
}XATTRVAL, *PXATTRVAL;

View File

@ -44,10 +44,10 @@ string s3fs_get_content_md5(int fd)
if(NULL == (base64 = s3fs_base64(md5hex, get_md5_digest_length()))){
return string(""); // ENOMEM
}
free(md5hex);
delete[] md5hex;
Signature = base64;
free(base64);
delete[] base64;
return Signature;
}
@ -62,7 +62,7 @@ string s3fs_md5sum(int fd, off_t start, ssize_t size)
}
std::string md5 = s3fs_hex(md5hex, digestlen);
free(md5hex);
delete[] md5hex;
return md5;
}
@ -81,7 +81,7 @@ string s3fs_sha256sum(int fd, off_t start, ssize_t size)
for(size_t pos = 0; pos < digestlen; pos++){
snprintf(sha256 + 2 * pos, 3, "%02x", sha256hex[pos]);
}
free(sha256hex);
delete[] sha256hex;
return string(sha256);
}

View File

@ -986,7 +986,7 @@ bool S3fsCurl::PushbackSseKeys(string& onekey)
if(NULL != (p_key = (char *)s3fs_decode64(onekey.c_str(), &keylength))) {
raw_key = string(p_key, keylength);
base64_key = onekey;
free(p_key);
delete[] p_key;
} else {
S3FS_PRN_ERR("Failed to convert base64 to SSE-C key %s", onekey.c_str());
return false;
@ -997,7 +997,7 @@ bool S3fsCurl::PushbackSseKeys(string& onekey)
if(NULL != (pbase64_key = s3fs_base64((unsigned char*)onekey.c_str(), onekey.length()))) {
raw_key = onekey;
base64_key = pbase64_key;
free(pbase64_key);
delete[] pbase64_key;
} else {
S3FS_PRN_ERR("Failed to convert base64 from SSE-C key %s", onekey.c_str());
return false;
@ -2436,7 +2436,7 @@ string S3fsCurl::CalcSignatureV2(const string& method, const string& strMD5, con
free(md);
Signature = base64;
free(base64);
delete[] base64;
return Signature;
}
@ -2479,9 +2479,9 @@ string S3fsCurl::CalcSignature(const string& method, const string& canonical_uri
s3fs_HMAC256(kDate, kDate_len, reinterpret_cast<const unsigned char*>(endpoint.c_str()), endpoint.size(), &kRegion, &kRegion_len);
s3fs_HMAC256(kRegion, kRegion_len, reinterpret_cast<const unsigned char*>("s3"), sizeof("s3") - 1, &kService, &kService_len);
s3fs_HMAC256(kService, kService_len, reinterpret_cast<const unsigned char*>("aws4_request"), sizeof("aws4_request") - 1, &kSigning, &kSigning_len);
free(kDate);
free(kRegion);
free(kService);
delete[] kDate;
delete[] kRegion;
delete[] kService;
const unsigned char* cRequest = reinterpret_cast<const unsigned char*>(StringCQ.c_str());
unsigned int cRequest_len = StringCQ.size();
@ -2489,7 +2489,7 @@ string S3fsCurl::CalcSignature(const string& method, const string& canonical_uri
for(cnt = 0; cnt < sRequest_len; cnt++){
sprintf(&hexsRequest[cnt * 2], "%02x", sRequest[cnt]);
}
free(sRequest);
delete[] sRequest;
StringToSign = "AWS4-HMAC-SHA256\n";
StringToSign += date8601 + "\n";
@ -2506,8 +2506,8 @@ string S3fsCurl::CalcSignature(const string& method, const string& canonical_uri
for(cnt = 0; cnt < md_len; cnt++){
sprintf(&hexSig[cnt * 2], "%02x", md[cnt]);
}
free(kSigning);
free(md);
delete[] kSigning;
delete[] md;
Signature = hexSig;
@ -2578,7 +2578,7 @@ void S3fsCurl::insertV4Headers()
for(cnt = 0; cnt < sRequest_len; cnt++){
sprintf(&hexsRequest[cnt * 2], "%02x", sRequest[cnt]);
}
free(sRequest);
delete[] sRequest;
payload_hash.assign(hexsRequest, &hexsRequest[sRequest_len * 2]);
break;
}
@ -3628,8 +3628,8 @@ int S3fsCurl::UploadMultipartPostSetup(const char* tpath, int part_num, const st
partdata.etag = s3fs_hex(md5raw, get_md5_digest_length());
char* md5base64p = s3fs_base64(md5raw, get_md5_digest_length());
requestHeaders = curl_slist_sort_insert(requestHeaders, "Content-MD5", md5base64p);
free(md5base64p);
free(md5raw);
delete[] md5base64p;
delete[] md5raw;
}
// make request

View File

@ -531,16 +531,12 @@ bool PageList::Serialize(CacheFileStat& file, bool is_output)
Init(0, false);
return true;
}
char* ptmp;
if(NULL == (ptmp = (char*)calloc(st.st_size + 1, sizeof(char)))){
S3FS_PRN_CRIT("could not allocate memory.");
S3FS_FUSE_EXIT();
return false;
}
char* ptmp = new char[st.st_size + 1];
ptmp[st.st_size] = '\0';
// read from file
if(0 >= pread(file.GetFd(), ptmp, st.st_size, 0)){
S3FS_PRN_ERR("failed to read stats(%d)", errno);
free(ptmp);
delete[] ptmp;
return false;
}
string oneline;
@ -552,7 +548,7 @@ bool PageList::Serialize(CacheFileStat& file, bool is_output)
// load(size)
if(!getline(ssall, oneline, '\n')){
S3FS_PRN_ERR("failed to parse stats.");
free(ptmp);
delete[] ptmp;
return false;
}
size_t total = s3fs_strtoofft(oneline.c_str());
@ -583,7 +579,7 @@ bool PageList::Serialize(CacheFileStat& file, bool is_output)
// add new area
SetPageLoadedStatus(offset, size, is_loaded);
}
free(ptmp);
delete[] ptmp;
if(is_err){
S3FS_PRN_ERR("failed to parse stats.");
Clear();

View File

@ -112,9 +112,7 @@ bool s3fs_HMAC(const void* key, size_t keylen, const unsigned char* data, size_t
return false;
}
if(NULL == (*digest = reinterpret_cast<unsigned char*>(malloc(SHA1_DIGEST_SIZE)))){
return false;
}
*digest = new unsigned char[SHA1_DIGEST_SIZE];
struct hmac_sha1_ctx ctx_hmac;
hmac_sha1_set_key(&ctx_hmac, keylen, reinterpret_cast<const uint8_t*>(key));
@ -131,9 +129,7 @@ bool s3fs_HMAC256(const void* key, size_t keylen, const unsigned char* data, siz
return false;
}
if(NULL == (*digest = reinterpret_cast<unsigned char*>(malloc(SHA256_DIGEST_SIZE)))){
return false;
}
*digest = new unsigned char[SHA256_DIGEST_SIZE];
struct hmac_sha256_ctx ctx_hmac;
hmac_sha256_set_key(&ctx_hmac, keylen, reinterpret_cast<const uint8_t*>(key));
@ -155,11 +151,9 @@ bool s3fs_HMAC(const void* key, size_t keylen, const unsigned char* data, size_t
if(0 == (*digestlen = gnutls_hmac_get_len(GNUTLS_MAC_SHA1))){
return false;
}
if(NULL == (*digest = reinterpret_cast<unsigned char*>(malloc(*digestlen + 1)))){
return false;
}
*digest = new unsigned char[*digestlen + 1];
if(0 > gnutls_hmac_fast(GNUTLS_MAC_SHA1, key, keylen, data, datalen, *digest)){
free(*digest);
delete[] *digest;
*digest = NULL;
return false;
}
@ -175,11 +169,9 @@ bool s3fs_HMAC256(const void* key, size_t keylen, const unsigned char* data, siz
if(0 == (*digestlen = gnutls_hmac_get_len(GNUTLS_MAC_SHA256))){
return false;
}
if(NULL == (*digest = reinterpret_cast<unsigned char*>(malloc(*digestlen + 1)))){
return false;
}
*digest = new unsigned char[*digestlen + 1];
if(0 > gnutls_hmac_fast(GNUTLS_MAC_SHA256, key, keylen, data, datalen, *digest)){
free(*digest);
delete[] *digest;
*digest = NULL;
return false;
}
@ -226,13 +218,11 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, ssize_t size)
md5_update(&ctx_md5, bytes, buf);
memset(buf, 0, 512);
}
if(NULL == (result = reinterpret_cast<unsigned char*>(malloc(get_md5_digest_length())))){
return NULL;
}
result = new unsigned char[get_md5_digest_length()];
md5_digest(&ctx_md5, get_md5_digest_length(), result);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}
@ -283,15 +273,12 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, ssize_t size)
gcry_md_write(ctx_md5, buf, bytes);
memset(buf, 0, 512);
}
if(NULL == (result = reinterpret_cast<unsigned char*>(malloc(get_md5_digest_length())))){
gcry_md_close(ctx_md5);
return NULL;
}
result = new unsigned char[get_md5_digest_length()];
memcpy(result, gcry_md_read(ctx_md5, 0), get_md5_digest_length());
gcry_md_close(ctx_md5);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}
@ -312,9 +299,7 @@ size_t get_sha256_digest_length()
bool s3fs_sha256(const unsigned char* data, unsigned int datalen, unsigned char** digest, unsigned int* digestlen)
{
(*digestlen) = static_cast<unsigned int>(get_sha256_digest_length());
if(NULL == ((*digest) = reinterpret_cast<unsigned char*>(malloc(*digestlen)))){
return false;
}
*digest = new unsigned char[*digestlen];
struct sha256_ctx ctx_sha256;
sha256_init(&ctx_sha256);
@ -353,13 +338,11 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
sha256_update(&ctx_sha256, bytes, buf);
memset(buf, 0, 512);
}
if(NULL == (result = reinterpret_cast<unsigned char*>(malloc(get_sha256_digest_length())))){
return NULL;
}
result = new unsigned char[get_sha256_digest_length()];
sha256_digest(&ctx_sha256, get_sha256_digest_length(), result);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}
@ -371,15 +354,13 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
bool s3fs_sha256(const unsigned char* data, unsigned int datalen, unsigned char** digest, unsigned int* digestlen)
{
size_t len = (*digestlen) = static_cast<unsigned int>(get_sha256_digest_length());
if(NULL == ((*digest) = reinterpret_cast<unsigned char*>(malloc(len)))){
return false;
}
*digest = new unsigned char[len];
gcry_md_hd_t ctx_sha256;
gcry_error_t err;
if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_sha256, GCRY_MD_SHA256, 0))){
S3FS_PRN_ERR("SHA256 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err));
free(*digest);
delete[] *digest;
return false;
}
gcry_md_write(ctx_sha256, data, datalen);
@ -431,15 +412,12 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
gcry_md_write(ctx_sha256, buf, bytes);
memset(buf, 0, 512);
}
if(NULL == (result = reinterpret_cast<unsigned char*>(malloc(get_sha256_digest_length())))){
gcry_md_close(ctx_sha256);
return NULL;
}
result = new unsigned char[get_sha256_digest_length()];
memcpy(result, gcry_md_read(ctx_sha256, 0), get_sha256_digest_length());
gcry_md_close(ctx_sha256);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}

View File

@ -127,9 +127,7 @@ static bool s3fs_HMAC_RAW(const void* key, size_t keylen, const unsigned char* d
PK11_FreeSymKey(pKey);
PK11_FreeSlot(Slot);
if(NULL == (*digest = reinterpret_cast<unsigned char*>(malloc(*digestlen)))){
return false;
}
*digest = new unsigned char[*digestlen];
memcpy(*digest, tmpdigest, *digestlen);
return true;
@ -192,15 +190,12 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, ssize_t size)
PK11_DigestOp(md5ctx, buf, bytes);
memset(buf, 0, 512);
}
if(NULL == (result = reinterpret_cast<unsigned char*>(malloc(get_md5_digest_length())))){
PK11_DestroyContext(md5ctx, PR_TRUE);
return NULL;
}
result = new unsigned char[get_md5_digest_length()];
PK11_DigestFinal(md5ctx, result, &md5outlen, get_md5_digest_length());
PK11_DestroyContext(md5ctx, PR_TRUE);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}
@ -218,9 +213,7 @@ size_t get_sha256_digest_length()
bool s3fs_sha256(const unsigned char* data, unsigned int datalen, unsigned char** digest, unsigned int* digestlen)
{
(*digestlen) = static_cast<unsigned int>(get_sha256_digest_length());
if(NULL == ((*digest) = reinterpret_cast<unsigned char*>(malloc(*digestlen)))){
return false;
}
*digest = new unsigned char[*digestlen];
PK11Context* sha256ctx;
unsigned int sha256outlen;
@ -273,15 +266,12 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
PK11_DigestOp(sha256ctx, buf, bytes);
memset(buf, 0, 512);
}
if(NULL == (result = reinterpret_cast<unsigned char*>(malloc(get_sha256_digest_length())))){
PK11_DestroyContext(sha256ctx, PR_TRUE);
return NULL;
}
result = new unsigned char[get_sha256_digest_length()];
PK11_DigestFinal(sha256ctx, result, &sha256outlen, get_sha256_digest_length());
PK11_DestroyContext(sha256ctx, PR_TRUE);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}

View File

@ -102,12 +102,7 @@ static unsigned long s3fs_crypt_get_threadid()
static struct CRYPTO_dynlock_value* s3fs_dyn_crypt_mutex(const char* file, int line)
{
struct CRYPTO_dynlock_value* dyndata;
if(NULL == (dyndata = static_cast<struct CRYPTO_dynlock_value*>(malloc(sizeof(struct CRYPTO_dynlock_value))))){
S3FS_PRN_CRIT("Could not allocate memory for CRYPTO_dynlock_value");
return NULL;
}
struct CRYPTO_dynlock_value* dyndata = new CRYPTO_dynlock_value();
pthread_mutex_init(&(dyndata->dyn_mutex), NULL);
return dyndata;
}
@ -127,7 +122,7 @@ static void s3fs_destroy_dyn_crypt_mutex(struct CRYPTO_dynlock_value* dyndata, c
{
if(dyndata){
pthread_mutex_destroy(&(dyndata->dyn_mutex));
free(dyndata);
delete dyndata;
}
}
@ -140,10 +135,7 @@ bool s3fs_init_crypt_mutex()
return false;
}
}
if(NULL == (s3fs_crypt_mutex = static_cast<pthread_mutex_t*>(malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t))))){
S3FS_PRN_CRIT("Could not allocate memory for s3fs_crypt_mutex");
return false;
}
s3fs_crypt_mutex = new pthread_mutex_t[CRYPTO_num_locks()];
for(int cnt = 0; cnt < CRYPTO_num_locks(); cnt++){
pthread_mutex_init(&s3fs_crypt_mutex[cnt], NULL);
}
@ -174,7 +166,7 @@ bool s3fs_destroy_crypt_mutex()
pthread_mutex_destroy(&s3fs_crypt_mutex[cnt]);
}
CRYPTO_cleanup_all_ex_data();
free(s3fs_crypt_mutex);
delete[] s3fs_crypt_mutex;
s3fs_crypt_mutex = NULL;
return true;
@ -189,9 +181,7 @@ static bool s3fs_HMAC_RAW(const void* key, size_t keylen, const unsigned char* d
return false;
}
(*digestlen) = EVP_MAX_MD_SIZE * sizeof(unsigned char);
if(NULL == ((*digest) = (unsigned char*)malloc(*digestlen))){
return false;
}
*digest = new unsigned char[*digestlen];
if(is_sha256){
HMAC(EVP_sha256(), key, keylen, data, datalen, *digest, digestlen);
}else{
@ -257,13 +247,11 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, ssize_t size)
memset(buf, 0, 512);
}
if(NULL == (result = (unsigned char*)malloc(get_md5_digest_length()))){
return NULL;
}
result = new unsigned char[get_md5_digest_length()];
MD5_Final(result, &md5ctx);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}
@ -281,9 +269,7 @@ size_t get_sha256_digest_length()
bool s3fs_sha256(const unsigned char* data, unsigned int datalen, unsigned char** digest, unsigned int* digestlen)
{
(*digestlen) = EVP_MAX_MD_SIZE * sizeof(unsigned char);
if(NULL == ((*digest) = reinterpret_cast<unsigned char*>(malloc(*digestlen)))){
return false;
}
*digest = new unsigned char[*digestlen];
const EVP_MD* md = EVP_get_digestbyname("sha256");
EVP_MD_CTX* mdctx = EVP_MD_CTX_create();
@ -335,15 +321,12 @@ unsigned char* s3fs_sha256hexsum(int fd, off_t start, ssize_t size)
EVP_DigestUpdate(sha256ctx, buf, bytes);
memset(buf, 0, 512);
}
if(NULL == (result = (unsigned char*)malloc(get_sha256_digest_length()))){
EVP_MD_CTX_destroy(sha256ctx);
return NULL;
}
result = new unsigned char[get_sha256_digest_length()];
EVP_DigestFinal_ex(sha256ctx, result, NULL);
EVP_MD_CTX_destroy(sha256ctx);
if(-1 == lseek(fd, start, SEEK_SET)){
free(result);
delete[] result;
return NULL;
}
return result;

View File

@ -3035,7 +3035,7 @@ static std::string build_xattrs(const xattrs_t& xattrs)
char* base64val = s3fs_base64((iter->second)->pvalue, (iter->second)->length);
if(base64val){
strxattrs += base64val;
free(base64val);
delete[] base64val;
}
}
strxattrs += '\"';
@ -3083,11 +3083,7 @@ static int set_xattrs_to_header(headers_t& meta, const char* name, const char* v
PXATTRVAL pval = new XATTRVAL;
pval->length = size;
if(0 < size){
if(NULL == (pval->pvalue = (unsigned char*)malloc(size))){
delete pval;
free_xattrs(xattrs);
return -ENOMEM;
}
pval->pvalue = new unsigned char[size];
memcpy(pval->pvalue, value, size);
}else{
pval->pvalue = NULL;

View File

@ -318,22 +318,17 @@ MVNODE *create_mvnode(const char *old_path, const char *new_path, bool is_dir, b
char *p_old_path;
char *p_new_path;
p = (MVNODE *) malloc(sizeof(MVNODE));
if (p == NULL) {
printf("create_mvnode: could not allocation memory for p\n");
S3FS_FUSE_EXIT();
return NULL;
}
p = new MVNODE();
if(NULL == (p_old_path = strdup(old_path))){
free(p);
delete p;
printf("create_mvnode: could not allocation memory for p_old_path\n");
S3FS_FUSE_EXIT();
return NULL;
}
if(NULL == (p_new_path = strdup(new_path))){
free(p);
delete p;
free(p_old_path);
printf("create_mvnode: could not allocation memory for p_new_path\n");
S3FS_FUSE_EXIT();
@ -417,7 +412,7 @@ void free_mvnodes(MVNODE *head)
next = my_head->next;
free(my_head->old_path);
free(my_head->new_path);
free(my_head);
delete my_head;
}
}
@ -475,33 +470,27 @@ string get_username(uid_t uid)
}
maxlen = res;
}
if(NULL == (pbuf = (char*)malloc(sizeof(char) * maxlen))){
S3FS_PRN_CRIT("failed to allocate memory.");
return string("");
}
pbuf = new char[maxlen];
// get pw information
while(ERANGE == (result = getpwuid_r(uid, &pwinfo, pbuf, maxlen, &ppwinfo))){
free(pbuf);
delete[] pbuf;
maxlen *= 2;
if(NULL == (pbuf = (char*)malloc(sizeof(char) * maxlen))){
S3FS_PRN_CRIT("failed to allocate memory.");
return string("");
}
pbuf = new char[maxlen];
}
if(0 != result){
S3FS_PRN_ERR("could not get pw information(%d).", result);
free(pbuf);
delete[] pbuf;
return string("");
}
// check pw
if(NULL == ppwinfo){
free(pbuf);
delete[] pbuf;
return string("");
}
string name = SAFESTRPTR(ppwinfo->pw_name);
free(pbuf);
delete[] pbuf;
return name;
}
@ -531,30 +520,24 @@ int is_uid_include_group(uid_t uid, gid_t gid)
}
maxlen = res;
}
if(NULL == (pbuf = (char*)malloc(sizeof(char) * maxlen))){
S3FS_PRN_CRIT("failed to allocate memory.");
return -ENOMEM;
}
pbuf = new char[maxlen];
// get group information
while(ERANGE == (result = getgrgid_r(gid, &ginfo, pbuf, maxlen, &pginfo))){
free(pbuf);
delete[] pbuf;
maxlen *= 2;
if(NULL == (pbuf = (char*)malloc(sizeof(char) * maxlen))){
S3FS_PRN_CRIT("failed to allocate memory.");
return -ENOMEM;
}
pbuf = new char[maxlen];
}
if(0 != result){
S3FS_PRN_ERR("could not get group information(%d).", result);
free(pbuf);
delete[] pbuf;
return -result;
}
// check group
if(NULL == pginfo){
// there is not gid in group.
free(pbuf);
delete[] pbuf;
return -EINVAL;
}
@ -564,11 +547,11 @@ int is_uid_include_group(uid_t uid, gid_t gid)
for(ppgr_mem = pginfo->gr_mem; ppgr_mem && *ppgr_mem; ppgr_mem++){
if(username == *ppgr_mem){
// Found username in group.
free(pbuf);
delete[] pbuf;
return 1;
}
}
free(pbuf);
delete[] pbuf;
return 0;
}

View File

@ -372,9 +372,7 @@ char* s3fs_base64(const unsigned char* input, size_t length)
if(!input || 0 == length){
return NULL;
}
if(NULL == (result = reinterpret_cast<char*>(malloc((((length / 3) + 1) * 4 + 1) * sizeof(char))))){
return NULL; // ENOMEM
}
result = new char[((length / 3) + 1) * 4 + 1];
unsigned char parts[4];
size_t rpos;
@ -422,9 +420,7 @@ unsigned char* s3fs_decode64(const char* input, size_t* plength)
if(!input || 0 == strlen(input) || !plength){
return NULL;
}
if(NULL == (result = reinterpret_cast<unsigned char*>(malloc((strlen(input) + 1))))){
return NULL; // ENOMEM
}
result = new unsigned char[strlen(input) + 1];
unsigned char parts[4];
size_t input_len = strlen(input);