From 72a1c65d003789fa9ad6330b8c6e6fe3ccb5eb42 Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Sun, 7 Jan 2018 00:30:18 +0100 Subject: [PATCH] Fix memory leaks in tests --- src/format/Kdbx3XmlReader.cpp | 2 +- src/format/Kdbx4Writer.cpp | 32 ++++++++++++++++---------------- tests/gui/TestGui.cpp | 9 ++++++++- tests/gui/TestGui.h | 9 +++++---- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/format/Kdbx3XmlReader.cpp b/src/format/Kdbx3XmlReader.cpp index 18dd0914d..fdc9cb416 100644 --- a/src/format/Kdbx3XmlReader.cpp +++ b/src/format/Kdbx3XmlReader.cpp @@ -1031,7 +1031,7 @@ Group* Kdbx3XmlReader::getGroup(const Uuid& uuid) if (m_groups.contains(uuid)) { return m_groups.value(uuid); } else { - Group* group = new Group(); + auto group = new Group(); group->setUpdateTimeinfo(false); group->setUuid(uuid); group->setParent(m_tmpParent); diff --git a/src/format/Kdbx4Writer.cpp b/src/format/Kdbx4Writer.cpp index 8b503c73a..fbd590563 100644 --- a/src/format/Kdbx4Writer.cpp +++ b/src/format/Kdbx4Writer.cpp @@ -114,24 +114,25 @@ bool Kdbx4Writer::writeDatabase(QIODevice* device, Database* db) CHECK_RETURN_FALSE(writeData(headerData)); QByteArray headerHash = CryptoHash::hash(headerData, CryptoHash::Sha256); - QScopedPointer firstLayer, secondLayer; - QByteArray hmacKey = KeePass2::hmacKey(masterSeed, db->transformedMasterKey()); QByteArray headerHmac = CryptoHash::hmac(headerData, HmacBlockStream::getHmacKey(UINT64_MAX, hmacKey), CryptoHash::Sha256); CHECK_RETURN_FALSE(writeData(headerHash)); CHECK_RETURN_FALSE(writeData(headerHmac)); - HmacBlockStream* hmacStream = new HmacBlockStream(device, hmacKey); - if (!hmacStream->open(QIODevice::WriteOnly)) { - raiseError(hmacStream->errorString()); + QScopedPointer hmacBlockStream; + QScopedPointer cipherStream; + + hmacBlockStream.reset(new HmacBlockStream(device, hmacKey)); + if (!hmacBlockStream->open(QIODevice::WriteOnly)) { + raiseError(hmacBlockStream->errorString()); return false; } - firstLayer.reset(static_cast(hmacStream)); - SymmetricCipherStream* cipherStream = new SymmetricCipherStream(hmacStream, algo, - SymmetricCipher::algorithmMode(algo), - SymmetricCipher::Encrypt); + cipherStream.reset(new SymmetricCipherStream(hmacBlockStream.data(), algo, + SymmetricCipher::algorithmMode(algo), + SymmetricCipher::Encrypt)); + if (!cipherStream->init(finalKey, encryptionIV)) { raiseError(cipherStream->errorString()); return false; @@ -140,13 +141,12 @@ bool Kdbx4Writer::writeDatabase(QIODevice* device, Database* db) raiseError(cipherStream->errorString()); return false; } - secondLayer.reset(static_cast(cipherStream)); QScopedPointer ioCompressor; if (db->compressionAlgo() == Database::CompressionNone) { - m_device = secondLayer.data(); + m_device = cipherStream.data(); } else { - ioCompressor.reset(new QtIOCompressor(secondLayer.data())); + ioCompressor.reset(new QtIOCompressor(cipherStream.data())); ioCompressor->setStreamFormat(QtIOCompressor::GzipFormat); if (!ioCompressor->open(QIODevice::WriteOnly)) { raiseError(ioCompressor->errorString()); @@ -191,12 +191,12 @@ bool Kdbx4Writer::writeDatabase(QIODevice* device, Database* db) if (ioCompressor) { ioCompressor->close(); } - if (!secondLayer->reset()) { - raiseError(secondLayer->errorString()); + if (!cipherStream->reset()) { + raiseError(cipherStream->errorString()); return false; } - if (!firstLayer->reset()) { - raiseError(firstLayer->errorString()); + if (!hmacBlockStream->reset()) { + raiseError(hmacBlockStream->errorString()); return false; } diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index bf237a854..d591165c4 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -117,7 +117,14 @@ void TestGui::cleanup() triggerAction("actionDatabaseClose"); Tools::wait(100); + if (m_db) { + delete m_db; + } m_db = nullptr; + + if (m_dbWidget) { + delete m_dbWidget; + } m_dbWidget = nullptr; } @@ -1060,7 +1067,7 @@ void TestGui::dragAndDropGroup(const QModelIndex& sourceIndex, const QModelIndex QVERIFY(sourceIndex.isValid()); QVERIFY(targetIndex.isValid()); - GroupModel* groupModel = qobject_cast(m_dbWidget->findChild("groupView")->model()); + auto groupModel = qobject_cast(m_dbWidget->findChild("groupView")->model()); QMimeData mimeData; QByteArray encoded; diff --git a/tests/gui/TestGui.h b/tests/gui/TestGui.h index 1a2b24e74..b8d3ce1fc 100644 --- a/tests/gui/TestGui.h +++ b/tests/gui/TestGui.h @@ -23,6 +23,7 @@ #include #include +#include class Database; class DatabaseTabWidget; @@ -71,14 +72,14 @@ private: void clickIndex(const QModelIndex& index, QAbstractItemView* view, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0); - MainWindow* m_mainWindow; - DatabaseTabWidget* m_tabWidget; - DatabaseWidget* m_dbWidget; + QPointer m_mainWindow; + QPointer m_tabWidget; + QPointer m_dbWidget; + QPointer m_db; QByteArray m_dbData; TemporaryFile m_dbFile; QString m_dbFileName; QString m_dbFilePath; - Database* m_db; }; #endif // KEEPASSX_TESTGUI_H