Skip to content

Commit

Permalink
fix: use safe functions to avoid UB under stacked borrows
Browse files Browse the repository at this point in the history
  • Loading branch information
muja committed Jul 25, 2023
1 parent 6e878f8 commit 940b98f
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,10 @@ impl Buffer {
/// if the position was more than 0, it is now 0
pub fn shift(&mut self) {
if self.position > 0 {
unsafe {
let length = self.end - self.position;
ptr::copy(
(&self.memory[self.position..self.end]).as_ptr(),
(&mut self.memory[..length]).as_mut_ptr(),
length,
);
self.position = 0;
self.end = length;
}
let length = self.end - self.position;
self.memory.copy_within(self.position..self.end, 0);
self.position = 0;
self.end = length;
}
}

Expand All @@ -233,16 +227,10 @@ impl Buffer {
return None;
}

unsafe {
let begin = self.position + start;
let next_end = self.end - length;
ptr::copy(
(&self.memory[begin + length..self.end]).as_ptr(),
(&mut self.memory[begin..next_end]).as_mut_ptr(),
self.end - (begin + length),
);
self.end = next_end;
}
let begin = self.position + start;
let next_end = self.end - length;
self.memory.copy_within(begin + length..self.end, begin);
self.end = next_end;
Some(self.available_data())
}

Expand Down Expand Up @@ -338,14 +326,8 @@ impl Write for Buffer {
impl Read for Buffer {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = cmp::min(self.available_data(), buf.len());
unsafe {
ptr::copy(
(&self.memory[self.position..self.position + len]).as_ptr(),
buf.as_mut_ptr(),
len,
);
self.position += len;
}
buf.copy_from_slice(&self.memory[self.position..self.position + len]);
self.position += len;
Ok(len)
}
}
Expand Down Expand Up @@ -429,7 +411,6 @@ mod tests {
assert_eq!(b.data(), &b"ab123Zgh"[..]);
}

use std::str;
#[test]
fn set_position() {
let mut output = [0; 5];
Expand Down

0 comments on commit 940b98f

Please sign in to comment.