From b1cf985e4409bd41cd3bc12996b7468e46b761c6 Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 26 Sep 2019 15:10:28 +0800 Subject: [PATCH] fix memory leak issue on Transcoder --- src/kiwano-audio/src/Transcoder.cpp | 18 +++++++++++++----- src/kiwano/core/vector.hpp | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/kiwano-audio/src/Transcoder.cpp b/src/kiwano-audio/src/Transcoder.cpp index e78a4db1..579fe721 100644 --- a/src/kiwano-audio/src/Transcoder.cpp +++ b/src/kiwano-audio/src/Transcoder.cpp @@ -260,12 +260,15 @@ namespace kiwano &sample_buffer_length ); - if (SUCCEEDED(hr) && sample_buffer_length <= max_stream_size) + if (position + sample_buffer_length >= max_stream_size) { - for (DWORD i = 0; i < sample_buffer_length; i++) - { - data[position++] = audio_data[i]; - } + hr = E_FAIL; + } + + if (SUCCEEDED(hr)) + { + ::memcpy(data + position, audio_data, sample_buffer_length); + position += sample_buffer_length; hr = buffer->Unlock(); } } @@ -281,6 +284,11 @@ namespace kiwano wave_data_ = data; wave_size_ = position; } + else + { + delete[] data; + data = nullptr; + } } } diff --git a/src/kiwano/core/vector.hpp b/src/kiwano/core/vector.hpp index 66011583..067e8b8f 100644 --- a/src/kiwano/core/vector.hpp +++ b/src/kiwano/core/vector.hpp @@ -225,12 +225,12 @@ namespace __vector_details static void copy_data(value_type* dest, size_type count, const value_type& val) { ::memset(dest, (Int32)val, (UInt32)count * sizeof(value_type)); } static void move_data(value_type* dest, const value_type* src, size_type count) { if (src == dest) return; ::memmove(dest, src, (UInt32)count * sizeof(value_type)); } - static value_type* allocate(size_type count) { return get_allocator().allocate(count); } - static void deallocate(value_type*& ptr, size_type count) { if (ptr) { get_allocator().deallocate(ptr, count); ptr = nullptr; } } + static value_type* allocate(size_type count) { return get_allocator().allocate(count); } + static void deallocate(value_type*& ptr, size_type count) { if (ptr) { get_allocator().deallocate(ptr, count); ptr = nullptr; } } - static void construct(value_type* ptr, size_type count) { } - static void construct(value_type* ptr, size_type count, const value_type& val) { while (count) { --count; *(ptr + count) = val; } } - static void destroy(value_type* ptr, size_type count) { } + static void construct(value_type* ptr, size_type count) { } + static void construct(value_type* ptr, size_type count, const value_type& val) { while (count) { --count; *(ptr + count) = val; } } + static void destroy(value_type* ptr, size_type count) { } private: static allocator_type& get_allocator() @@ -269,12 +269,12 @@ namespace __vector_details } } - static value_type* allocate(size_type count) { return get_allocator().allocate(count); } - static void deallocate(value_type*& ptr, size_type count) { if (ptr) { get_allocator().deallocate(ptr, count); ptr = nullptr; } } + static value_type* allocate(size_type count) { return get_allocator().allocate(count); } + static void deallocate(value_type*& ptr, size_type count) { if (ptr) { get_allocator().deallocate(ptr, count); ptr = nullptr; } } - static void construct(value_type* ptr, size_type count) { construct(ptr, count, value_type()); } - static void construct(value_type* ptr, size_type count, const value_type& val) { while (count) get_allocator().construct(ptr + (--count), val); } - static void destroy(value_type* ptr, size_type count) { while (count) get_allocator().destroy(ptr + (--count)); } + static void construct(value_type* ptr, size_type count) { construct(ptr, count, value_type()); } + static void construct(value_type* ptr, size_type count, const value_type& val) { while (count) get_allocator().construct(ptr + (--count), val); } + static void destroy(value_type* ptr, size_type count) { while (count) get_allocator().destroy(ptr + (--count)); } private: static allocator_type& get_allocator()