From fe0dc508ac5e5b5852cdd5db44dd94e37a7a0d83 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 20 Mar 2020 15:35:45 +0100 Subject: [PATCH] Allow for zero-length wiggle_runtime::Regions (#1366) Previously, we'd be very strict and disallow zero-length `wiggle_runtime::Region`s altogether (we'd actually panic which is even worse). However, we should allow this noting that any zero-length `Region` never overlaps since its length is, well, zero. Additionally, this makes `path_readlink` with zero buffers cleaner and possible without additional checks/hacks around the passed in `GuestPtr<'_, [u8]>` buffer. --- crates/wiggle/crates/runtime/src/region.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/wiggle/crates/runtime/src/region.rs b/crates/wiggle/crates/runtime/src/region.rs index e1e6084dc0..58555abcd2 100644 --- a/crates/wiggle/crates/runtime/src/region.rs +++ b/crates/wiggle/crates/runtime/src/region.rs @@ -7,12 +7,16 @@ pub struct Region { impl Region { pub fn new(start: u32, len: u32) -> Self { - assert!(len > 0, "Region cannot have 0 length"); Self { start, len } } /// Checks if this `Region` overlaps with `rhs` `Region`. pub fn overlaps(&self, rhs: Region) -> bool { + // Zero-length regions can never overlap! + if self.len == 0 || rhs.len == 0 { + return false; + } + let self_start = self.start as u64; let self_end = self_start + (self.len - 1) as u64; @@ -39,6 +43,17 @@ impl Region { mod test { use super::*; + #[test] + fn zero_length() { + let r1 = Region::new(0, 0); + let r2 = Region::new(0, 1); + assert!(!r1.overlaps(r2)); + + let r1 = Region::new(0, 1); + let r2 = Region::new(0, 0); + assert!(!r1.overlaps(r2)); + } + #[test] fn nonoverlapping() { let r1 = Region::new(0, 10);