mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-21 02:08:12 +03:00
Kernel/Memory: Use scope guard to remove a region if we failed to map it
This commit is contained in:
parent
23902d46f1
commit
a1a1462a22
Notes:
sideshowbarker
2024-07-18 03:23:00 +09:00
Author: https://github.com/supercomputer7 Commit: https://github.com/SerenityOS/serenity/commit/a1a1462a22 Pull-request: https://github.com/SerenityOS/serenity/pull/14901 Reviewed-by: https://github.com/IdanHo
@ -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, ®ion]() {
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user