From fea3433cfc42d1a7ed050779092f3c92b077cd48 Mon Sep 17 00:00:00 2001 From: Tomohiro Ubukata Date: Sun, 15 Mar 2020 05:22:41 +0000 Subject: [PATCH] Add error checks --- .../core/platform/cloud/curl_http_request.cc | 8 +++-- .../platform/default/posix_file_system.cc | 14 ++++++-- .../core/platform/default/subprocess.cc | 36 ++++++++++++++----- tensorflow/core/platform/path.cc | 4 ++- tensorflow/core/platform/platform_strings.cc | 4 ++- .../android_armv7a_cpu_utils_helper.cc | 8 +++-- 6 files changed, 56 insertions(+), 18 deletions(-) diff --git a/tensorflow/core/platform/cloud/curl_http_request.cc b/tensorflow/core/platform/cloud/curl_http_request.cc index a227edb1fb0..39a1f7a35b2 100644 --- a/tensorflow/core/platform/cloud/curl_http_request.cc +++ b/tensorflow/core/platform/cloud/curl_http_request.cc @@ -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_) { diff --git a/tensorflow/core/platform/default/posix_file_system.cc b/tensorflow/core/platform/default/posix_file_system.cc index 106a0412fb7..05c2b2762d4 100644 --- a/tensorflow/core/platform/default/posix_file_system.cc +++ b/tensorflow/core/platform/default/posix_file_system.cc @@ -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(); } diff --git a/tensorflow/core/platform/default/subprocess.cc b/tensorflow/core/platform/default/subprocess.cc index 562f4cd2d0c..acf7073b9a4 100644 --- a/tensorflow/core/platform/default/subprocess.cc +++ b/tensorflow/core/platform/default/subprocess.cc @@ -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)) { diff --git a/tensorflow/core/platform/path.cc b/tensorflow/core/platform/path.cc index 1e88328aace..a041ac67d72 100644 --- a/tensorflow/core/platform/path.cc +++ b/tensorflow/core/platform/path.cc @@ -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; } } diff --git a/tensorflow/core/platform/platform_strings.cc b/tensorflow/core/platform/platform_strings.cc index 489a211ccf7..af8787f4fbc 100644 --- a/tensorflow/core/platform/platform_strings.cc +++ b/tensorflow/core/platform/platform_strings.cc @@ -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; } diff --git a/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc b/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc index 0534443d17c..f75d3533d17 100644 --- a/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc +++ b/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc @@ -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 }