Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix two buffer overruns reachable from safe code #231

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/stream/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,19 @@ impl Operation for NoOp {
// Skip the prelude
let src = &input.src[input.pos..];
// Safe because `output.pos() <= output.dst.capacity()`.
let dst = unsafe { output.dst.as_mut_ptr().add(output.pos()) };
let output_pos = output.pos();
let dst = unsafe { output.dst.as_mut_ptr().add(output_pos) };

// Ignore anything past the end
let len = usize::min(src.len(), output.dst.capacity());
let len = usize::min(src.len(), output.dst.capacity() - output_pos);
let src = &src[..len];

// Safe because:
// * `len` is less than either of the two lengths
// * `src` and `dst` do not overlap because we have `&mut` to each.
unsafe { std::ptr::copy_nonoverlapping(src.as_ptr(), dst, len) };
input.set_pos(input.pos() + len);
unsafe { output.set_pos(output.pos() + len) };
unsafe { output.set_pos(output_pos + len) };

Ok(0)
}
Expand Down
1 change: 1 addition & 0 deletions zstd-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,7 @@ impl<'a, C: WriteBuf + ?Sized> OutBuffer<'a, C> {

/// Returns the current cursor position.
pub fn pos(&self) -> usize {
assert!(self.pos <= self.dst.capacity());
self.pos
}

Expand Down
Loading