Add error checks

This commit is contained in:
Tomohiro Ubukata 2020-03-15 05:22:41 +00:00
parent 0e0fc5a791
commit fea3433cfc
6 changed files with 56 additions and 18 deletions

View File

@ -166,7 +166,9 @@ CurlHttpRequest::~CurlHttpRequest() {
libcurl_->curl_slist_free_all(resolve_list_);
}
if (put_body_) {
fclose(put_body_);
if (fclose(put_body_) != 0) {
LOG(ERROR) << "fclose() failed: " << strerror(errno);
}
}
if (curl_) {
libcurl_->curl_easy_cleanup(curl_);
@ -237,7 +239,9 @@ Status CurlHttpRequest::SetPutFromFile(const string& body_filepath,
is_method_set_ = true;
method_ = RequestMethod::kPut;
if (put_body_) {
fclose(put_body_);
if (fclose(put_body_) != 0) {
LOG(ERROR) << "fclose() failed: " << strerror(errno);
}
}
put_body_ = fopen(body_filepath.c_str(), "r");
if (!put_body_) {

View File

@ -51,7 +51,11 @@ class PosixRandomAccessFile : public RandomAccessFile {
public:
PosixRandomAccessFile(const string& fname, int fd)
: filename_(fname), fd_(fd) {}
~PosixRandomAccessFile() override { close(fd_); }
~PosixRandomAccessFile() override {
if (close(fd_) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
}
Status Name(StringPiece* result) const override {
*result = filename_;
@ -229,7 +233,9 @@ Status PosixFileSystem::NewReadOnlyMemoryRegionFromFile(
} else {
result->reset(new PosixReadOnlyMemoryRegion(address, st.st_size));
}
close(fd);
if (close(fd) < 0) {
s = IOError(fname, errno);
}
}
return s;
}
@ -256,7 +262,9 @@ Status PosixFileSystem::GetChildren(const string& dir,
result->push_back(entry->d_name);
}
}
closedir(d);
if (closedir(d) < 0) {
return IOError(dir, errno);
}
return Status::OK();
}

View File

@ -102,11 +102,15 @@ void SubProcess::FreeArgs() {
void SubProcess::ClosePipes() {
for (int i = 0; i < kNFds; i++) {
if (parent_pipe_[i] >= 0) {
close(parent_pipe_[i]);
if (close(parent_pipe_[i]) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
parent_pipe_[i] = -1;
}
if (child_pipe_[i] >= 0) {
close(child_pipe_[i]);
if (close(child_pipe_[i]) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
child_pipe_[i] = -1;
}
}
@ -215,7 +219,9 @@ bool SubProcess::Start() {
running_ = true;
for (int i = 0; i < kNFds; i++) {
if (child_pipe_[i] >= 0) {
close(child_pipe_[i]);
if (close(child_pipe_[i]) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
child_pipe_[i] = -1;
}
}
@ -227,7 +233,9 @@ bool SubProcess::Start() {
int devnull_fd = -1;
for (int i = 0; i < kNFds; i++) {
if (parent_pipe_[i] >= 0) {
close(parent_pipe_[i]);
if (close(parent_pipe_[i]) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
parent_pipe_[i] = -1;
}
@ -242,7 +250,9 @@ bool SubProcess::Start() {
_exit(1);
}
}
close(child_pipe_[i]);
if (close(child_pipe_[i]) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
child_pipe_[i] = -1;
break;
@ -264,14 +274,18 @@ bool SubProcess::Start() {
}
}
} else {
close(i);
if (close(i) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
}
break;
}
}
if (devnull_fd >= 0) {
close(devnull_fd);
if (close(devnull_fd) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
}
// Execute the child program.
@ -379,7 +393,9 @@ int SubProcess::Communicate(const string* stdin_input, string* stdout_output,
// Special case: if no data is given to send to the child process,
// close the pipe to unblock the child, and skip the file descriptor.
if (stdin_input == nullptr) {
close(parent_pipe_[i]);
if (close(parent_pipe_[i]) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
parent_pipe_[i] = -1;
continue;
}
@ -441,7 +457,9 @@ int SubProcess::Communicate(const string* stdin_input, string* stdout_output,
fds[i].fd = -1;
fd_remain--;
// Close the child's stdin pipe to unblock the process.
close(parent_pipe_[CHAN_STDIN]);
if (close(parent_pipe_[CHAN_STDIN]) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
parent_pipe_[CHAN_STDIN] = -1;
}
} else if (!retry(errno)) {

View File

@ -320,7 +320,9 @@ string GetTempFilename(const string& extension) {
if (fd < 0) {
LOG(FATAL) << "Failed to create temp file.";
} else {
close(fd);
if (close(fd) < 0) {
LOG(ERROR) << "close() failed: " << strerror(errno);
}
return tmp_filepath;
}
}

View File

@ -52,7 +52,9 @@ int GetPlatformStrings(const std::string& path,
}
result = (ferror(ifp) == 0) ? 0 : errno;
fclose(ifp);
if (fclose(ifp) != 0) {
result = errno;
}
} else {
result = errno;
}

View File

@ -118,10 +118,14 @@ int64 AndroidArmV7ACpuUtilsHelper::ReadCpuFrequencyFile(
const int retval = fscanf(fp, "%lld", &freq_in_khz);
if (retval < 0) {
LOG(WARNING) << "Failed to \"" << file_path << "\"";
fclose(fp);
if (fclose(fp) != 0) {
LOG(WARNING) << "fclose() failed: " << strerror(errno);
}
return INVALID_CPU_FREQUENCY;
}
fclose(fp);
if (fclose(fp) != 0) {
LOG(WARNING) << "fclose() failed: " << strerror(errno);
}
return freq_in_khz * 1000; // The file contains cpu frequency in khz
}