Skip to content

Commit 7c28b6c

Browse files
committed
get_slices: panic if GuestMemoryRegion violates contract
Panic in get_slices() if the call to .get_slice() returns a slice that does not match the `len` parameter passed to it. If this happens there is an application bug somewhere in the GuestMemoryRegion implementation, and the library code in vm-memory, while not doing anything unsound, will probably react unpredictably to this situation. This way, the final paragraph in the get_slices() documentatoin about cumulative lengths is actually true. Signed-off-by: Patrick Roy <[email protected]>
1 parent a1a157f commit 7c28b6c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/guest_memory.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,13 @@ impl<'a, M: GuestMemory + ?Sized> GuestMemorySliceIterator<'a, M> {
525525
(_, true) => return Some(Err(Error::GuestAddressOverflow)),
526526
};
527527

528-
Some(region.get_slice(start, len))
528+
Some(region.get_slice(start, len).inspect(|s| {
529+
assert_eq!(
530+
s.len(),
531+
len,
532+
"get_slice() returned a slice with wrong length"
533+
)
534+
}))
529535
}
530536
}
531537

0 commit comments

Comments
 (0)