Fixed memory leaks in non-gui tests

Fixed 2 memory leaks in production code and a few in testcases. As a
result leak_check_at_exit ASAN option does not need to turned off for
non-gui tests.
Smart pointers should be used elsewhere for consistency, but the sooner
this fixes are delivered, the lesser memory leaks are introduced.
This commit is contained in:
Michal Kaptur 2017-11-27 21:41:58 +01:00 committed by Janek Bevendorff
parent b20918b60e
commit 0ff75e7a88
10 changed files with 125 additions and 167 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (C) 2016 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -18,36 +19,29 @@
#include "KeePass2Repair.h"
#include <QBuffer>
#include <QScopedPointer>
#include <QRegExp>
#include "format/KeePass2RandomStream.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2XmlReader.h"
KeePass2Repair::KeePass2Repair()
: m_db(nullptr)
KeePass2Repair::RepairOutcome KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
{
}
KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
{
m_db = nullptr;
m_errorStr.clear();
KeePass2Reader reader;
reader.setSaveXml(true);
Database* db = reader.readDatabase(device, key, true);
QScopedPointer<Database> db(reader.readDatabase(device, key, true));
if (!reader.hasError()) {
delete db;
return NothingTodo;
return qMakePair(NothingTodo, nullptr);
}
QByteArray xmlData = reader.xmlData();
if (!db || xmlData.isEmpty()) {
delete db;
m_errorStr = reader.errorString();
return UnableToOpen;
return qMakePair(UnableToOpen, nullptr);
}
bool repairAction = false;
@ -59,8 +53,7 @@ KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, c
&& encodingRegExp.cap(1).compare("utf8", Qt::CaseInsensitive) != 0)
{
// database is not utf-8 encoded, we don't support repairing that
delete db;
return RepairFailed;
return qMakePair(RepairFailed, nullptr);
}
}
@ -75,8 +68,7 @@ KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, c
if (!repairAction) {
// we were unable to find the problem
delete db;
return RepairFailed;
return qMakePair(RepairFailed, nullptr);
}
KeePass2RandomStream randomStream;
@ -84,23 +76,16 @@ KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, c
KeePass2XmlReader xmlReader;
QBuffer buffer(&xmlData);
buffer.open(QIODevice::ReadOnly);
xmlReader.readDatabase(&buffer, db, &randomStream);
xmlReader.readDatabase(&buffer, db.data(), &randomStream);
if (xmlReader.hasError()) {
delete db;
return RepairFailed;
return qMakePair(RepairFailed, nullptr);
}
else {
m_db = db;
return RepairSuccess;
return qMakePair(RepairSuccess, db.take());
}
}
Database* KeePass2Repair::database() const
{
return m_db;
}
QString KeePass2Repair::errorString() const
{
return m_errorStr;

View file

@ -1,5 +1,6 @@
/*
* Copyright (C) 2016 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -20,6 +21,7 @@
#include <QCoreApplication>
#include <QIODevice>
#include <QPair>
#include "core/Database.h"
#include "keys/CompositeKey.h"
@ -36,14 +38,12 @@ public:
RepairSuccess,
RepairFailed
};
using RepairOutcome = QPair<RepairResult, Database*>;
KeePass2Repair();
RepairResult repairDatabase(QIODevice* device, const CompositeKey& key);
Database* database() const;
RepairOutcome repairDatabase(QIODevice* device, const CompositeKey& key);
QString errorString() const;
private:
Database* m_db;
QString m_errorStr;
};