Remove several calls to free (#2308)

This commit is contained in:
Andrew Gaul 2023-09-06 23:50:33 +09:00 committed by GitHub
parent 5f38301861
commit fa3a472c6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 17 deletions

View File

@ -71,6 +71,7 @@ struct curl_slist* curl_slist_sort_insert(struct curl_slist* list, const char* k
} }
struct curl_slist* new_item; struct curl_slist* new_item;
// Must use malloc since curl_slist_free_all calls free.
if(nullptr == (new_item = static_cast<struct curl_slist*>(malloc(sizeof(*new_item))))){ if(nullptr == (new_item = static_cast<struct curl_slist*>(malloc(sizeof(*new_item))))){
free(data); free(data);
return list; return list;

View File

@ -23,6 +23,7 @@
#include <iostream> #include <iostream>
#include <climits> #include <climits>
#include <list> #include <list>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -53,7 +54,7 @@ const char usage_string[] = "Usage : \"write_multiblock -f <file path> -p <start
//--------------------------------------------------------- //---------------------------------------------------------
// Utility functions // Utility functions
//--------------------------------------------------------- //---------------------------------------------------------
static unsigned char* create_random_data(off_t size) static std::unique_ptr<unsigned char[]> create_random_data(off_t size)
{ {
int fd; int fd;
if(-1 == (fd = open("/dev/urandom", O_RDONLY))){ if(-1 == (fd = open("/dev/urandom", O_RDONLY))){
@ -61,17 +62,11 @@ static unsigned char* create_random_data(off_t size)
return nullptr; return nullptr;
} }
unsigned char* pbuff; std::unique_ptr<unsigned char[]> pbuff(new unsigned char[size]);
if(nullptr == (pbuff = reinterpret_cast<unsigned char*>(malloc(size)))){
std::cerr << "[ERROR] Could not allocate memory." << std::endl;
close(fd);
return nullptr;
}
for(ssize_t readpos = 0, readcnt = 0; readpos < size; readpos += readcnt){ for(ssize_t readpos = 0, readcnt = 0; readpos < size; readpos += readcnt){
if(-1 == (readcnt = read(fd, &(pbuff[readpos]), static_cast<size_t>(size - readpos)))){ if(-1 == (readcnt = read(fd, &(pbuff[readpos]), static_cast<size_t>(size - readpos)))){
if(EAGAIN != errno && EWOULDBLOCK != errno && EINTR != errno){ if(EAGAIN != errno && EWOULDBLOCK != errno && EINTR != errno){
std::cerr << "[ERROR] Failed reading from /dev/urandom with errno: " << errno << std::endl; std::cerr << "[ERROR] Failed reading from /dev/urandom with errno: " << errno << std::endl;
free(pbuff);
close(fd); close(fd);
return nullptr; return nullptr;
} }
@ -206,10 +201,7 @@ int main(int argc, char** argv)
} }
// make data and buffer // make data and buffer
unsigned char* pData; std::unique_ptr<unsigned char[]> pData = create_random_data(max_size);
if(nullptr == (pData = create_random_data(max_size))){
exit(EXIT_FAILURE);
}
for(strlist_t::const_iterator fiter = files.begin(); fiter != files.end(); ++fiter){ for(strlist_t::const_iterator fiter = files.begin(); fiter != files.end(); ++fiter){
// open/create file // open/create file
@ -218,18 +210,15 @@ int main(int argc, char** argv)
if(0 == stat(fiter->c_str(), &st)){ if(0 == stat(fiter->c_str(), &st)){
if(!S_ISREG(st.st_mode)){ if(!S_ISREG(st.st_mode)){
std::cerr << "[ERROR] File " << *fiter << " is existed, but it is not regular file." << std::endl; std::cerr << "[ERROR] File " << *fiter << " is existed, but it is not regular file." << std::endl;
free(pData);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(-1 == (fd = open(fiter->c_str(), O_WRONLY))){ if(-1 == (fd = open(fiter->c_str(), O_WRONLY))){
std::cerr << "[ERROR] Could not open " << *fiter << std::endl; std::cerr << "[ERROR] Could not open " << *fiter << std::endl;
free(pData);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
}else{ }else{
if(-1 == (fd = open(fiter->c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644))){ if(-1 == (fd = open(fiter->c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644))){
std::cerr << "[ERROR] Could not create " << *fiter << std::endl; std::cerr << "[ERROR] Could not create " << *fiter << std::endl;
free(pData);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -242,7 +231,6 @@ int main(int argc, char** argv)
if(EAGAIN != errno && EWOULDBLOCK != errno && EINTR != errno){ if(EAGAIN != errno && EWOULDBLOCK != errno && EINTR != errno){
std::cerr << "[ERROR] Failed writing to " << *fiter << " by errno : " << errno << std::endl; std::cerr << "[ERROR] Failed writing to " << *fiter << " by errno : " << errno << std::endl;
close(fd); close(fd);
free(pData);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
writecnt = 0; writecnt = 0;
@ -252,7 +240,6 @@ int main(int argc, char** argv)
// close file // close file
close(fd); close(fd);
} }
free(pData);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }