diff options
author | 2017-12-10 19:44:54 -0600 | |
---|---|---|
committer | 2017-12-10 20:44:54 -0500 | |
commit | e17ae378063b46c894a8c193823f029d7d87de81 (patch) | |
tree | 58ba9d0c2377b4f1894a8676245896f727f6324d | |
parent | 76a9354d2d90ef8f2fa6562d3d8ef6bc973d56a3 (diff) |
Fix several potential memory leaks (#14816)
* Fix several potential memory leaks
This fix fixes several potential memory leaks, mostly
caused by error return without proper deleting.
Note: The original issue was raised by @orpillar, thanks!
This fix fixes 14800.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
4 files changed, 6 insertions, 3 deletions
diff --git a/tensorflow/c/c_api.cc b/tensorflow/c/c_api.cc index 13253ced49..8a85eba5fc 100644 --- a/tensorflow/c/c_api.cc +++ b/tensorflow/c/c_api.cc @@ -579,6 +579,7 @@ TF_Tensor* TF_TensorFromTensor(const tensorflow::Tensor& src, status->status = InvalidArgument( "invalid string tensor encoding (string #", i, " of ", srcarray.size(), "): ", status->status.error_message()); + delete[] base; return nullptr; } dst += consumed; @@ -588,6 +589,7 @@ TF_Tensor* TF_TensorFromTensor(const tensorflow::Tensor& src, status->status = InvalidArgument( "invalid string tensor encoding (decoded ", (dst - base), " bytes, but the tensor is encoded in ", size, " bytes"); + delete[] base; return nullptr; } diff --git a/tensorflow/core/lib/io/snappy/snappy_outputbuffer.cc b/tensorflow/core/lib/io/snappy/snappy_outputbuffer.cc index be1fa22c69..3c31016732 100644 --- a/tensorflow/core/lib/io/snappy/snappy_outputbuffer.cc +++ b/tensorflow/core/lib/io/snappy/snappy_outputbuffer.cc @@ -161,7 +161,7 @@ Status SnappyOutputBuffer::Deflate() { } // Write length of compressed block to output buffer. - char* compressed_length_array = new char[4]; + char compressed_length_array[4]; std::fill(compressed_length_array, compressed_length_array + 4, 0); for (int i = 0; i < 4; i++) { // Little endian. @@ -173,7 +173,6 @@ Status SnappyOutputBuffer::Deflate() { TF_RETURN_IF_ERROR(AddToOutputBuffer(output.data(), output.size())); next_in_ += avail_in_; avail_in_ = 0; - delete[] compressed_length_array; return Status::OK(); } 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 fb1955edde..12dc9c58b3 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,9 +118,10 @@ int64 AndroidArmV7ACpuUtilsHelper::ReadCpuFrequencyFile( const int retval = fscanf(fp, "%lld", &freq_in_khz); if (retval < 0) { LOG(WARNING) << "Failed to \"" << file_path << "\""; + fclose(fp); return INVALID_CPU_FREQUENCY; } - pclose(fp); + fclose(fp); return freq_in_khz * 1000; // The file contains cpu frequency in khz } diff --git a/tensorflow/tools/proto_text/gen_proto_text_functions.cc b/tensorflow/tools/proto_text/gen_proto_text_functions.cc index ecb29a65a0..f0bb59acf8 100644 --- a/tensorflow/tools/proto_text/gen_proto_text_functions.cc +++ b/tensorflow/tools/proto_text/gen_proto_text_functions.cc @@ -132,6 +132,7 @@ int MainImpl(int argc, char** argv) { FILE* f = fopen(path.c_str(), "w"); if (f == nullptr) return -1; if (fwrite(data.c_str(), 1, data.size(), f) != data.size()) { + fclose(f); return -1; } if (fclose(f) != 0) { |