From 64a18747e21b244d38c1a8d2682692f07d1e46bd Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 27 Nov 2017 11:13:45 +0100 Subject: Safeguards to avoid crashes --- framework/src/domain/composercontroller.cpp | 30 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'framework/src') diff --git a/framework/src/domain/composercontroller.cpp b/framework/src/domain/composercontroller.cpp index aa8dea5d..75db7346 100644 --- a/framework/src/domain/composercontroller.cpp +++ b/framework/src/domain/composercontroller.cpp @@ -102,13 +102,15 @@ static void traverse(const QStandardItemModel *model, const std::function -void asyncRun(std::function run, std::function continuation) +void asyncRun(QObject *object, std::function run, std::function continuation) { + auto guard = QPointer{object}; auto future = QtConcurrent::run(run); auto watcher = new QFutureWatcher; - //FIXME holding on to the item pointer like this is not safe - QObject::connect(watcher, &QFutureWatcher::finished, watcher, [watcher, continuation]() { - continuation(watcher->future().result()); + QObject::connect(watcher, &QFutureWatcher::finished, watcher, [watcher, continuation, guard]() { + if (guard) { + continuation(watcher->future().result()); + } delete watcher; }); watcher->setFuture(future); @@ -129,7 +131,8 @@ public: void add(const QString &addressee) { auto item = new QStandardItem; - item->setData(addressee, ComposerController::AddresseeNameRole); + item->setText(addressee); + item->setData(QVariant::fromValue(addressee), ComposerController::AddresseeNameRole); item->setData(false, ComposerController::KeyFoundRole); item->setData(mCryptoEnabled, ComposerController::KeyMissingRole); appendRow(QList() << item); @@ -144,7 +147,7 @@ public: mb.fromUnicodeString(addressee); SinkLog() << "Searching key for: " << mb.address(); - asyncRun>([mb] { + asyncRun>(this, [mb] { auto keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, false, MailCrypto::OPENPGP); if (keys.empty()) { //Search for key on remote server if it's missing and import @@ -154,15 +157,16 @@ public: } return keys; }, - //FIXME holding on to the item pointer like this is not safe - [this, item](const std::vector &keys) { + [this, addressee](const std::vector &keys) { if (!keys.empty()) { if (keys.size() > 1 ) { SinkWarning() << "Found more than one key, picking first one."; } SinkLog() << "Found key: " << keys.front().primaryFingerprint(); - item->setData(true, ComposerController::KeyFoundRole); - item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole); + for (auto item : findItems(addressee)) { + item->setData(true, ComposerController::KeyFoundRole); + item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole); + } } else { SinkWarning() << "Failed to find key for recipient."; } @@ -173,7 +177,7 @@ public: { auto root = invisibleRootItem(); for (int row = 0; row < root->rowCount(); row++) { - if (root->child(row, 0)->data(ComposerController::AddresseeNameRole).toString() == addressee) { + if (root->child(row, 0)->text() == addressee) { root->removeRow(row); return; } @@ -214,7 +218,7 @@ public: { QStringList list; traverse(this, [&] (QStandardItem *item) { - list << item->data(ComposerController::AddresseeNameRole).toString(); + list << item->text(); }); return list; } @@ -225,7 +229,7 @@ public: traverse(this, [&] (QStandardItem *item) { item->setData(mCryptoEnabled, ComposerController::KeyMissingRole); if (mCryptoEnabled) { - findKey(item->data(ComposerController::AddresseeNameRole).toString(), item); + findKey(item->text(), item); } }); } -- cgit v1.2.3