From a56abf68dd4fd1bfacecfac82841c7210a5d1184 Mon Sep 17 00:00:00 2001 From: Jnewbon <48688400+Jnewbon@users.noreply.github.com> Date: Mon, 29 Apr 2019 13:56:08 +0100 Subject: [PATCH] #6488 Fixed a memory leak in the TLS socket code --- src/lib/net/SecureSocket.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index 43b45e2a..21d9bbdf 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -199,6 +199,7 @@ SecureSocket::doWrite() { static bool s_retry = false; static int s_retrySize = 0; + static int s_staticBufferSize = 0; static void* s_staticBuffer = NULL; // write data @@ -211,8 +212,13 @@ SecureSocket::doWrite() } else { bufferSize = m_outputBuffer.getSize(); - s_staticBuffer = malloc(bufferSize); - memcpy(s_staticBuffer, m_outputBuffer.peek(bufferSize), bufferSize); + if (bufferSize != 0) { + if (bufferSize > s_staticBufferSize) { + s_staticBuffer = realloc(s_staticBuffer, bufferSize); + s_staticBufferSize = bufferSize; + } + memcpy(s_staticBuffer, m_outputBuffer.peek(bufferSize), bufferSize); + } } if (bufferSize == 0) { @@ -224,8 +230,6 @@ SecureSocket::doWrite() if (status > 0) { s_retry = false; bufferSize = 0; - free(s_staticBuffer); - s_staticBuffer = NULL; } else if (status < 0) { return kBreak;