From 4d13d52710fa497d17bca5148ccf0ee6b9685893 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov <71632867+SerhiiGadzhilov@users.noreply.github.com> Date: Fri, 6 May 2022 21:55:06 +0300 Subject: [PATCH] SYNERGY1-1497 Fix memory leaks (#7164) * SYNERGY1-1497 Fix problem with memory leak * SYNERGY1-1497 Fix memory leak for TLS * Update ChangeLog * SYNERGY-1-1497 Update ChangeLog and Copyright info --- ChangeLog | 1 + src/lib/net/SecureSocket.cpp | 43 +++++++++++++++------------- src/lib/net/SecureSocket.h | 3 +- src/lib/platform/OSXAutoTypes.h | 28 ++++++++++++++++++ src/lib/platform/OSXKeyState.cpp | 4 +-- src/lib/platform/OSXKeyState.h | 14 ++++----- src/lib/synergy/unix/AppUtilUnix.cpp | 9 +++--- 7 files changed, 66 insertions(+), 36 deletions(-) create mode 100644 src/lib/platform/OSXAutoTypes.h diff --git a/ChangeLog b/ChangeLog index ad973539e..d2c6670fc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,7 @@ Bug fixes: - #7144 Fix double lines when pasting text from Linux to Windows - #7149 Address issues with modifiers and dead keys - #7163 Fix compilation issues for FreeBSD +- #7164 Fix memory leaks in language sync and TLS functionality Github Actions: - #7148 Fix unstable build for windows core diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index 4cc2a1274..6760b7de4 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -77,31 +77,13 @@ SecureSocket::SecureSocket(IEventQueue* events, SecureSocket::~SecureSocket() { - isFatal(true); - // take socket from multiplexer ASAP otherwise the race condition - // could cause events to get called on a dead object. TCPSocket - // will do this, too, but the double-call is harmless - setJob(NULL); - if (m_ssl->m_ssl != NULL) { - SSL_shutdown(m_ssl->m_ssl); - - SSL_free(m_ssl->m_ssl); - m_ssl->m_ssl = NULL; - } - if (m_ssl->m_context != NULL) { - SSL_CTX_free(m_ssl->m_context); - m_ssl->m_context = NULL; - } - delete m_ssl; + freeSSL(); } void SecureSocket::close() { - isFatal(true); - - SSL_shutdown(m_ssl->m_ssl); - + freeSSL(); TCPSocket::close(); } @@ -415,6 +397,27 @@ SecureSocket::createSSL() } } +void +SecureSocket::freeSSL() +{ + isFatal(true); + // take socket from multiplexer ASAP otherwise the race condition + // could cause events to get called on a dead object. TCPSocket + // will do this, too, but the double-call is harmless + setJob(NULL); + if (m_ssl->m_ssl != NULL) { + SSL_shutdown(m_ssl->m_ssl); + + SSL_free(m_ssl->m_ssl); + m_ssl->m_ssl = NULL; + } + if (m_ssl->m_context != NULL) { + SSL_CTX_free(m_ssl->m_context); + m_ssl->m_context = NULL; + } + delete m_ssl; +} + int SecureSocket::secureAccept(int socket) { diff --git a/src/lib/net/SecureSocket.h b/src/lib/net/SecureSocket.h index e20c133a2..f31a459ea 100644 --- a/src/lib/net/SecureSocket.h +++ b/src/lib/net/SecureSocket.h @@ -67,6 +67,7 @@ private: // SSL void initContext(bool server); void createSSL(); + void freeSSL(); int secureAccept(int s); int secureConnect(int s); bool showCertificate(); @@ -90,7 +91,7 @@ private: void showSecureConnectInfo(); void showSecureLibInfo(); void showSecureCipherInfo(); - + void handleTCPConnected(const Event& event, void*); private: diff --git a/src/lib/platform/OSXAutoTypes.h b/src/lib/platform/OSXAutoTypes.h new file mode 100644 index 000000000..fae0a1dc2 --- /dev/null +++ b/src/lib/platform/OSXAutoTypes.h @@ -0,0 +1,28 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2022 Symless Ltd. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#if WINAPI_CARBON +#include +#include + +using CFDeallocator = decltype(&CFRelease); +using AutoCFArray = std::unique_ptr; +using AutoCFDictionary = std::unique_ptr; +using AutoTISInputSourceRef = std::unique_ptr<__TISInputSource, CFDeallocator>; + +#endif diff --git a/src/lib/platform/OSXKeyState.cpp b/src/lib/platform/OSXKeyState.cpp index 5b614845c..c04f879d9 100644 --- a/src/lib/platform/OSXKeyState.cpp +++ b/src/lib/platform/OSXKeyState.cpp @@ -910,13 +910,13 @@ OSXKeyState::handleModifierKey(void* target, } bool -OSXKeyState::getGroups(GroupList& groups) const +OSXKeyState::getGroups(AutoCFArray& groups) const { // get number of layouts CFStringRef keys[] = { kTISPropertyInputSourceCategory }; CFStringRef values[] = { kTISCategoryKeyboardInputSource }; AutoCFDictionary dict(CFDictionaryCreate(NULL, (const void **)keys, (const void **)values, 1, NULL, NULL), CFRelease); - GroupList kbds(TISCreateInputSourceList(dict.get(), false), CFRelease); + AutoCFArray kbds(TISCreateInputSourceList(dict.get(), false), CFRelease); if (CFArrayGetCount(kbds.get()) > 0) { groups = std::move(kbds); diff --git a/src/lib/platform/OSXKeyState.h b/src/lib/platform/OSXKeyState.h index 92ac7217c..20edea1ff 100644 --- a/src/lib/platform/OSXKeyState.h +++ b/src/lib/platform/OSXKeyState.h @@ -22,6 +22,7 @@ #include "common/stdmap.h" #include "common/stdset.h" #include "common/stdvector.h" +#include "OSXAutoTypes.h" #include @@ -107,11 +108,6 @@ protected: private: class KeyResource; - typedef void(*CFDeallocator)(CFTypeRef); - typedef std::unique_ptr GroupList; - typedef std::unique_ptr AutoCFDictionary; - typedef std::unique_ptr<__TISInputSource, CFDeallocator> AutoTISInputSourceRef; - // Add hard coded special keys to a synergy::KeyMap. void getKeyMapForSpecialKeys( @@ -122,7 +118,7 @@ private: SInt32 group, const IOSXKeyResource& r) const; // Get the available keyboard groups - bool getGroups(GroupList&) const; + bool getGroups(AutoCFArray&) const; // Change active keyboard group to group void setGroup(SInt32 group); @@ -176,9 +172,9 @@ private: typedef std::map GroupMap; typedef std::map VirtualKeyMap; - VirtualKeyMap m_virtualKeyMap; - mutable UInt32 m_deadKeyState; - GroupList m_groups{nullptr, CFRelease}; + VirtualKeyMap m_virtualKeyMap; + mutable UInt32 m_deadKeyState; + AutoCFArray m_groups{nullptr, CFRelease}; GroupMap m_groupMap; bool m_shiftPressed; bool m_controlPressed; diff --git a/src/lib/synergy/unix/AppUtilUnix.cpp b/src/lib/synergy/unix/AppUtilUnix.cpp index a3187b8c9..14e8ef378 100644 --- a/src/lib/synergy/unix/AppUtilUnix.cpp +++ b/src/lib/synergy/unix/AppUtilUnix.cpp @@ -25,6 +25,7 @@ #include #elif WINAPI_CARBON #include +#include #else #error Platform not supported. #endif @@ -71,11 +72,11 @@ AppUtilUnix::getKeyboardLayoutList() #elif WINAPI_CARBON CFStringRef keys[] = { kTISPropertyInputSourceCategory }; CFStringRef values[] = { kTISCategoryKeyboardInputSource }; - CFDictionaryRef dict = CFDictionaryCreate(NULL, (const void **)keys, (const void **)values, 1, NULL, NULL); - CFArrayRef kbds = TISCreateInputSourceList(dict, false); + AutoCFDictionary dict(CFDictionaryCreate(NULL, (const void **)keys, (const void **)values, 1, NULL, NULL), CFRelease); + AutoCFArray kbds(TISCreateInputSourceList(dict.get(), false), CFRelease); - for (CFIndex i = 0; i < CFArrayGetCount(kbds); ++i) { - TISInputSourceRef keyboardLayout = (TISInputSourceRef)CFArrayGetValueAtIndex(kbds, i); + for (CFIndex i = 0; i < CFArrayGetCount(kbds.get()); ++i) { + TISInputSourceRef keyboardLayout = (TISInputSourceRef)CFArrayGetValueAtIndex(kbds.get(), i); auto layoutLanguages = (CFArrayRef)TISGetInputSourceProperty(keyboardLayout, kTISPropertyInputSourceLanguages); char temporaryCString[128] = {0}; for(CFIndex index = 0; index < CFArrayGetCount(layoutLanguages) && layoutLanguages; index++) {