mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:52:45 +00: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
					
				
					 1 changed files with 9 additions and 8 deletions
				
			
		|  | @ -211,20 +211,21 @@ ErrorOr<Region*> AddressSpace::allocate_region_with_vmobject(RandomizeVirtualAdd | ||||||
|     else |     else | ||||||
|         TRY(m_region_tree.place_specifically(*region, VirtualRange { VirtualAddress { requested_address }, size })); |         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) { |     if (prot == PROT_NONE) { | ||||||
|         // For PROT_NONE mappings, we don't have to set up any page table mappings.
 |         // 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.
 |         // We do still need to attach the region to the page_directory though.
 | ||||||
|         region->set_page_directory(page_directory()); |         region->set_page_directory(page_directory()); | ||||||
|     } else { |     } else { | ||||||
|         auto result = region->map(page_directory(), ShouldFlushTLB::No); |         TRY(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(); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |     remove_region_from_tree_on_failure.disarm(); | ||||||
|     return region.leak_ptr(); |     return region.leak_ptr(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Liav A
						Liav A