Browse Source

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.
pull/1367/head
Jakub Konka 5 years ago
committed by GitHub
parent
commit
fe0dc508ac
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      crates/wiggle/crates/runtime/src/region.rs

17
crates/wiggle/crates/runtime/src/region.rs

@ -7,12 +7,16 @@ pub struct Region {
impl Region { impl Region {
pub fn new(start: u32, len: u32) -> Self { pub fn new(start: u32, len: u32) -> Self {
assert!(len > 0, "Region cannot have 0 length");
Self { start, len } Self { start, len }
} }
/// Checks if this `Region` overlaps with `rhs` `Region`. /// Checks if this `Region` overlaps with `rhs` `Region`.
pub fn overlaps(&self, rhs: Region) -> bool { 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_start = self.start as u64;
let self_end = self_start + (self.len - 1) as u64; let self_end = self_start + (self.len - 1) as u64;
@ -39,6 +43,17 @@ impl Region {
mod test { mod test {
use super::*; 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] #[test]
fn nonoverlapping() { fn nonoverlapping() {
let r1 = Region::new(0, 10); let r1 = Region::new(0, 10);

Loading…
Cancel
Save