Skip to content

Commit

Permalink
refactor: use safe version of unimportant unsafe code
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo committed Jul 16, 2024
1 parent 256bef5 commit 533b8ca
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions commons/zenoh-keyexpr/src/key_expr/canon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub trait Canonizable {
fn canonize(&mut self);
}

// Return the length of the canonized string
fn canonize(bytes: &mut [u8]) -> usize {
let mut index = 0;
let mut written = 0;
Expand Down Expand Up @@ -96,8 +97,7 @@ impl Canonizable for &mut str {
// and remaining garbage bytes are zeroed
let bytes = unsafe { self.as_bytes_mut() };
let length = canonize(bytes);
// SAFETY: canonized length is lesser than or equal to the original length
unsafe { bytes.get_unchecked_mut(length..) }.fill(b'\0');
bytes[length..].fill(b'\0');
}
}

Expand All @@ -107,8 +107,7 @@ impl Canonizable for String {
// and remaining garbage bytes are truncated
let bytes = unsafe { self.as_mut_vec() };
let length = canonize(bytes);
// SAFETY: canonized length is lesser than or equal to the original length
unsafe { bytes.set_len(length) };
bytes.truncate(length);
}
}

Expand Down

0 comments on commit 533b8ca

Please sign in to comment.