Skip to content

Commit

Permalink
loosen memory area shrink check
Browse files Browse the repository at this point in the history
  • Loading branch information
aarkegz committed Aug 26, 2024
1 parent 27bc903 commit 6e7ca94
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions memory_set/src/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,16 @@ impl<B: MappingBackend> MemoryArea<B> {
/// The start address of the memory area is increased by `new_size`. The
/// shrunk part is unmapped.
///
/// `new_size` must be greater than 0 and less than the current size,
/// otherwise this function returns an error.
/// `new_size` must be greater than 0 and less than the current size.
pub(crate) fn shrink_left(
&mut self,
new_size: usize,
page_table: &mut B::PageTable,
) -> MappingResult {
assert!(new_size > 0 && new_size < self.size());

let old_size = self.size();
let unmap_size = old_size
.checked_sub(new_size)
.filter(|&s| s > 0 && s < old_size)
.ok_or(MappingError::InvalidParam)?;
let unmap_size = old_size - new_size;

if !self.backend.unmap(self.start(), unmap_size, page_table) {
return Err(MappingError::BadState);
Expand All @@ -131,18 +129,15 @@ impl<B: MappingBackend> MemoryArea<B> {
/// The end address of the memory area is decreased by `new_size`. The
/// shrunk part is unmapped.
///
/// `new_size` must be greater than 0 and less than the current size,
/// otherwise this function returns an error.
/// `new_size` must be greater than 0 and less than the current size.
pub(crate) fn shrink_right(
&mut self,
new_size: usize,
page_table: &mut B::PageTable,
) -> MappingResult {
assert!(new_size > 0 && new_size < self.size());
let old_size = self.size();
let unmap_size = old_size
.checked_sub(new_size)
.filter(|&s| s > 0 && s < old_size)
.ok_or(MappingError::InvalidParam)?;
let unmap_size = old_size - new_size;

// Use wrapping_add to avoid overflow check.
// Safety: `new_size` is less than the current size, so it will never overflow.
Expand Down

0 comments on commit 6e7ca94

Please sign in to comment.