From f4b207220c60b9bbc6d4ea414a419768b8913c12 Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 2 Aug 2022 15:38:51 -0300 Subject: [PATCH] fix: add some more nullptr checks / protection die sigsegv :gun: Signed-off-by: flow --- launcher/modplatform/EnsureMetadataTask.cpp | 44 ++++++++++++++++----- launcher/modplatform/EnsureMetadataTask.h | 4 +- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/launcher/modplatform/EnsureMetadataTask.cpp b/launcher/modplatform/EnsureMetadataTask.cpp index 1f49d7c9..11bdb99d 100644 --- a/launcher/modplatform/EnsureMetadataTask.cpp +++ b/launcher/modplatform/EnsureMetadataTask.cpp @@ -24,8 +24,10 @@ EnsureMetadataTask::EnsureMetadataTask(Mod* mod, QDir dir, ModPlatform::Provider : Task(nullptr), m_index_dir(dir), m_provider(prov), m_hashing_task(nullptr), m_current_task(nullptr) { auto hash_task = createNewHash(mod); + if (!hash_task) + return; connect(hash_task.get(), &Task::succeeded, [this, hash_task, mod] { m_mods.insert(hash_task->getResult(), mod); }); - connect(hash_task.get(), &Task::failed, [this, hash_task, mod] { emitFail(mod, RemoveFromList::No); }); + connect(hash_task.get(), &Task::failed, [this, hash_task, mod] { emitFail(mod, "", RemoveFromList::No); }); hash_task->start(); } @@ -38,14 +40,14 @@ EnsureMetadataTask::EnsureMetadataTask(QList& mods, QDir dir, ModPlatform: if (!hash_task) continue; connect(hash_task.get(), &Task::succeeded, [this, hash_task, mod] { m_mods.insert(hash_task->getResult(), mod); }); - connect(hash_task.get(), &Task::failed, [this, hash_task, mod] { emitFail(mod, RemoveFromList::No); }); + connect(hash_task.get(), &Task::failed, [this, hash_task, mod] { emitFail(mod, "", RemoveFromList::No); }); m_hashing_task->addTask(hash_task); } } Hashing::Hasher::Ptr EnsureMetadataTask::createNewHash(Mod* mod) { - if (!mod->valid() || mod->type() == Mod::MOD_FOLDER) + if (!mod || !mod->valid() || mod->type() == Mod::MOD_FOLDER) return nullptr; return Hashing::createHasher(mod->fileinfo().absoluteFilePath(), m_provider); @@ -120,7 +122,7 @@ void EnsureMetadataTask::executeTask() QMutableHashIterator mods_iter(m_mods); while (mods_iter.hasNext()) { auto mod = mods_iter.next(); - emitFail(mod.value()); + emitFail(mod.value(), mod.key()); } emitSucceeded(); @@ -168,22 +170,44 @@ void EnsureMetadataTask::executeTask() version_task->start(); } -void EnsureMetadataTask::emitReady(Mod* m, RemoveFromList remove) +void EnsureMetadataTask::emitReady(Mod* m, QString key, RemoveFromList remove) { + if (!m) { + qCritical() << "Tried to mark a null mod as ready."; + if (!key.isEmpty()) + m_mods.remove(key); + + return; + } + qDebug() << QString("Generated metadata for %1").arg(m->name()); emit metadataReady(m); - if (remove == RemoveFromList::Yes) - m_mods.remove(getExistingHash(m)); + if (remove == RemoveFromList::Yes) { + if (key.isEmpty()) + key = getExistingHash(m); + m_mods.remove(key); + } } -void EnsureMetadataTask::emitFail(Mod* m, RemoveFromList remove) +void EnsureMetadataTask::emitFail(Mod* m, QString key, RemoveFromList remove) { + if (!m) { + qCritical() << "Tried to mark a null mod as failed."; + if (!key.isEmpty()) + m_mods.remove(key); + + return; + } + qDebug() << QString("Failed to generate metadata for %1").arg(m->name()); emit metadataFailed(m); - if (remove == RemoveFromList::Yes) - m_mods.remove(getExistingHash(m)); + if (remove == RemoveFromList::Yes) { + if (key.isEmpty()) + key = getExistingHash(m); + m_mods.remove(key); + } } // Modrinth diff --git a/launcher/modplatform/EnsureMetadataTask.h b/launcher/modplatform/EnsureMetadataTask.h index 13319266..a8b0851e 100644 --- a/launcher/modplatform/EnsureMetadataTask.h +++ b/launcher/modplatform/EnsureMetadataTask.h @@ -39,8 +39,8 @@ class EnsureMetadataTask : public Task { Yes, No }; - void emitReady(Mod*, RemoveFromList = RemoveFromList::Yes); - void emitFail(Mod*, RemoveFromList = RemoveFromList::Yes); + void emitReady(Mod*, QString key = {}, RemoveFromList = RemoveFromList::Yes); + void emitFail(Mod*, QString key = {}, RemoveFromList = RemoveFromList::Yes); // Hashes and stuff auto createNewHash(Mod*) -> Hashing::Hasher::Ptr;