[network/krdc] rdp: Leak fix: most obvious leaks detected with ASAN

Fabio Bas <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 50f74f635532b3e4abf0ee7f0f6664c37ff3ec46 by Fabio Bas.
Committed on 17/07/2026 at 08:50.
Pushed by ctrlaltca into branch 'master'.

Leak fix: most obvious leaks detected with ASAN
Authored-by: akallabeth <[email protected]>

M  +19   -6    rdp/rdpcliprdr.cpp
M  +12   -0    rdp/rdpsession.cpp

https://invent.kde.org/network/krdc/-/commit/50f74f635532b3e4abf0ee7f0f6664c37ff3ec46

diff --git a/rdp/rdpcliprdr.cpp b/rdp/rdpcliprdr.cpp
index 2bda6c68..bd6f267b 100644
--- a/rdp/rdpcliprdr.cpp
+++ b/rdp/rdpcliprdr.cpp
@@ -10,6 +10,17 @@
 #include "rdpview.h"
 #include <freerdp/version.h>
 
+static void cliprdr_format_free(CLIPRDR_FORMAT *formats, size_t count)
+{
+    if (!formats)
+        return;
+
+    for (size_t x = 0; x < count; x++)
+        free(formats->formatName);
+
+    delete[] formats;
+}
+
 UINT RdpClipboard::onSendClientFormatList(CliprdrClientContext *cliprdr)
 {
     auto kclip = reinterpret_cast<RdpClipboard *>(cliprdr->custom);
@@ -21,11 +32,11 @@ UINT RdpClipboard::onSendClientFormatList(CliprdrClientContext *cliprdr)
 
     UINT32 *pFormatIds = nullptr;
     UINT32 numFormats = ClipboardGetFormatIds(kclip->m_clipboard, &pFormatIds);
-    CLIPRDR_FORMAT *formats = reinterpret_cast<CLIPRDR_FORMAT *>(calloc(numFormats, sizeof(CLIPRDR_FORMAT)));
+    auto formats = new CLIPRDR_FORMAT[numFormats];
 
     if (!formats) {
         free(pFormatIds);
-        free(formats);
+        cliprdr_format_free(formats, numFormats);
         return ERROR_INTERNAL_ERROR;
     }
 
@@ -40,7 +51,7 @@ UINT RdpClipboard::onSendClientFormatList(CliprdrClientContext *cliprdr)
 
             if (!formats[index].formatName) {
                 free(pFormatIds);
-                free(formats);
+                cliprdr_format_free(formats, numFormats);
                 return ERROR_INTERNAL_ERROR;
             }
         }
@@ -54,13 +65,15 @@ UINT RdpClipboard::onSendClientFormatList(CliprdrClientContext *cliprdr)
 
     if (!cliprdr->ClientFormatList) {
         free(pFormatIds);
-        free(formats);
+        if (formats)
+            free(formats->formatName);
+        delete[] formats;
         return ERROR_INTERNAL_ERROR;
     }
 
     auto rc = cliprdr->ClientFormatList(cliprdr, &formatList);
     free(pFormatIds);
-    free(formats);
+    cliprdr_format_free(formats, numFormats);
     return rc;
 }
 
@@ -352,4 +365,4 @@ bool RdpClipboard::sendClipboard(const QMimeData *data)
     }
 
     return false;
-}
\ No newline at end of file
+}
diff --git a/rdp/rdpsession.cpp b/rdp/rdpsession.cpp
index 8f1e3ffc..c55ed4ce 100644
--- a/rdp/rdpsession.cpp
+++ b/rdp/rdpsession.cpp
@@ -1130,10 +1130,17 @@ bool RdpSession::onAuthenticate(char **username, char **password, char **domain)
     Q_UNUSED(domain);
 
     if (m_firstPasswordTry && m_user.size()) {
+        free(*username);
         *username = _strdup(m_user.toUtf8().data());
+
+        free(*domain);
+        *domain = nullptr;
         if (m_domain.size()) {
             *domain = _strdup(m_domain.toUtf8().data());
         }
+
+        free(*password);
+        *password = nullptr;
         if (m_password.size()) {
             *password = _strdup(m_password.toUtf8().data());
             m_firstPasswordTry = false;
@@ -1143,8 +1150,11 @@ bool RdpSession::onAuthenticate(char **username, char **password, char **domain)
 
     Q_EMIT onAuthRequested();
 
+    free(*username);
     *username = _strdup(m_user.toUtf8().data());
+    free(*domain);
     *domain = _strdup(m_domain.toUtf8().data());
+    free(*password);
     *password = _strdup(m_password.toUtf8().data());
 
     return true;
@@ -1168,6 +1178,7 @@ void RdpSession::run()
     LARGE_INTEGER due;
     due.QuadPart = 0;
     if (!SetWaitableTimer(timer, &due, 1, nullptr, nullptr, false)) {
+        CloseHandle(timer);
         return;
     }
 
@@ -1191,6 +1202,7 @@ void RdpSession::run()
     }
 
     freerdp_disconnect(instance);
+    CloseHandle(timer);
 }
 
 void RdpSession::emitErrorMessage()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.