fix a lot of ub in disownPatch

This commit is contained in:
dankmeme01 2024-04-13 14:15:09 +02:00
parent 65e0023df8
commit 4c492c1f60
2 changed files with 15 additions and 8 deletions
loader/src/loader

View file

@ -600,16 +600,16 @@ Result<> Mod::Impl::disownPatch(Patch* patch) {
"A patch that was getting disowned had its owner set but the owner "
"didn't have the patch in m_patches.");
m_patches.erase(foundIt);
if (!this->isEnabled() || !patch->getAutoEnable())
return Ok();
auto res2 = patch->disable();
if (!res2) {
return Err("Cannot disable patch: {}", res2.unwrapErr());
if (this->isEnabled() && patch->getAutoEnable()) {
auto res2 = patch->disable();
if (!res2) {
return Err("Cannot disable patch: {}", res2.unwrapErr());
}
}
m_patches.erase(foundIt);
return Ok();
}

View file

@ -70,8 +70,15 @@ Result<> Patch::Impl::enable() {
Result<> Patch::Impl::disable() {
auto res = tulip::hook::writeMemory(m_address, m_original.data(), m_original.size());
if (!res) return Err("Failed to disable patch: {}", res.unwrapErr());
m_enabled = false;
allEnabled().erase(std::find(allEnabled().begin(), allEnabled().end(), this));
auto it = std::find(allEnabled().begin(), allEnabled().end(), this);
if (it == allEnabled().end()) {
return Err("Failed to disable patch: patch is already disabled");
}
allEnabled().erase(it);
return Ok();
}