Merge pull request #37749 from t-ubukata:bug-error-check

PiperOrigin-RevId: 316929300
Change-Id: I39196dc247da04fbec47f30123ca10d0c8cc4613
This commit is contained in:
TensorFlower Gardener 2020-06-17 11:36:19 -07:00
commit 587ff8b556
6 changed files with 56 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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