[network/ruqola] src/core: Fix mem leak

Laurent Montel <[email protected]> Tue, 4 Aug 2026 12:23:16 +0000 (UTC)
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 2d6ab041664bc94a79ab7c36f10d0976da6f594f by Laurent Montel.
Committed on 04/08/2026 at 12:23.
Pushed by mlaurent into branch 'master'.

Fix mem leak

M  +8    -2    src/core/autotests/sessionkeytest.cpp
M  +2    -0    src/core/encryption/encryptionutils.h

https://invent.kde.org/network/ruqola/-/commit/2d6ab041664bc94a79ab7c36f10d0976da6f594f

diff --git a/src/core/autotests/sessionkeytest.cpp b/src/core/autotests/sessionkeytest.cpp
index fea717109d..0457dc5790 100644
--- a/src/core/autotests/sessionkeytest.cpp
+++ b/src/core/autotests/sessionkeytest.cpp
@@ -7,6 +7,7 @@
 #include "sessionkeytest.h"
 #include "encryption/encryptionutils.h"
 #include <QTest>
+#include <memory>
 
 QTEST_GUILESS_MAIN(SessionKeyTest)
 SessionKeyTest::SessionKeyTest(QObject *parent)
@@ -34,8 +35,13 @@ void SessionKeyTest::sessionKeyEncryptionDecryptionTest()
 
     for (int i = 0; i <= 10; i++) {
         const QByteArray sessionKey = EncryptionUtils::generateSessionKey();
-        const QByteArray encryptedSessionKey = EncryptionUtils::encryptSessionKey(sessionKey, EncryptionUtils::publicKeyFromPEM(publicKey));
-        const QByteArray decryptedSessionKey = EncryptionUtils::decryptSessionKey(encryptedSessionKey, EncryptionUtils::privateKeyFromPEM(privateKey));
+        std::unique_ptr<RSA, decltype(&RSA_free)> publicRsaKey(EncryptionUtils::publicKeyFromPEM(publicKey), &RSA_free);
+        QVERIFY(publicRsaKey);
+        const QByteArray encryptedSessionKey = EncryptionUtils::encryptSessionKey(sessionKey, publicRsaKey.get());
+
+        std::unique_ptr<RSA, decltype(&RSA_free)> privateRsaKey(EncryptionUtils::privateKeyFromPEM(privateKey), &RSA_free);
+        QVERIFY(privateRsaKey);
+        const QByteArray decryptedSessionKey = EncryptionUtils::decryptSessionKey(encryptedSessionKey, privateRsaKey.get());
         QCOMPARE(sessionKey, decryptedSessionKey);
     }
 }
diff --git a/src/core/encryption/encryptionutils.h b/src/core/encryption/encryptionutils.h
index 76d17f18eb..2c09c72876 100644
--- a/src/core/encryption/encryptionutils.h
+++ b/src/core/encryption/encryptionutils.h
@@ -46,7 +46,9 @@ struct RSAKeyPair {
 [[nodiscard]] LIBRUQOLACORE_EXPORT QByteArray generateSessionKey();
 [[nodiscard]] LIBRUQOLACORE_EXPORT QByteArray encryptSessionKey(const QByteArray &sessionKey, RSA *publicKey);
 [[nodiscard]] LIBRUQOLACORE_EXPORT QByteArray decryptSessionKey(const QByteArray &encryptedSessionKey, RSA *privateKey);
+// Caller owns the returned RSA object and must release it with RSA_free().
 [[nodiscard]] LIBRUQOLACORE_EXPORT RSA *publicKeyFromPEM(const QByteArray &pem);
+// Caller owns the returned RSA object and must release it with RSA_free().
 [[nodiscard]] LIBRUQOLACORE_EXPORT RSA *privateKeyFromPEM(const QByteArray &pem);
 [[nodiscard]] LIBRUQOLACORE_EXPORT QString generateRandomText(int size);
 [[nodiscard]] LIBRUQOLACORE_EXPORT EncryptionUtils::EncryptionInfo splitVectorAndEcryptedData(const QByteArray &cipherText);