Kernel/Memory: Use scope guard to remove a region if we failed to map it

This commit is contained in:
Liav A 2022-08-17 19:47:33 +03:00 committed by Idan Horowitz
parent 23902d46f1
commit a1a1462a22
Notes: sideshowbarker 2024-07-18 03:23:00 +09:00

View File

@ -211,20 +211,21 @@ ErrorOr<Region*> AddressSpace::allocate_region_with_vmobject(RandomizeVirtualAdd
else
TRY(m_region_tree.place_specifically(*region, VirtualRange { VirtualAddress { requested_address }, size }));
ArmedScopeGuard remove_region_from_tree_on_failure = [this, &region]() {
// At this point the region is already part of the Process region tree, so we have to make sure
// we remove it from the tree before returning an error, or else the Region tree will contain
// a dangling pointer to the free'd Region instance
m_region_tree.remove(*region);
};
if (prot == PROT_NONE) {
// For PROT_NONE mappings, we don't have to set up any page table mappings.
// We do still need to attach the region to the page_directory though.
region->set_page_directory(page_directory());
} else {
auto result = region->map(page_directory(), ShouldFlushTLB::No);
if (result.is_error()) [[unlikely]] {
// At this point the region is already part of the Process region tree, so we have to make sure
// we remove it from the tree before returning this error, or else the Region tree will contain
// a dangling pointer to the free'd Region instance
m_region_tree.remove(*region);
return result.release_error();
}
TRY(region->map(page_directory(), ShouldFlushTLB::No));
}
remove_region_from_tree_on_failure.disarm();
return region.leak_ptr();
}