Skip to content
New issue

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

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

Already on GitHub? # to your account

Drop unsafe code from truncate_old_buffer() #32

Merged
merged 3 commits into from
Jun 24, 2019
Merged
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
9 changes: 4 additions & 5 deletions src/deflate/decode.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ use byteorder::ReadBytesExt;
use std::cmp;
use std::io;
use std::io::Read;
use std::ptr;

use super::symbol;
use bit;
@@ -135,11 +134,11 @@ where
}
fn truncate_old_buffer(&mut self) {
if self.buffer.len() > lz77::MAX_DISTANCE as usize * 4 {
let old_len = self.buffer.len();
let new_len = lz77::MAX_DISTANCE as usize;
unsafe {
let ptr = self.buffer.as_mut_ptr();
let src = ptr.add(self.buffer.len() - new_len);
ptr::copy_nonoverlapping(src, ptr, new_len);
{ // isolation to please borrow checker
let (dst, src) = self.buffer.split_at_mut(old_len - new_len);
dst[..new_len].copy_from_slice(src);
}
self.buffer.truncate(new_len);
self.offset = new_len;
9 changes: 4 additions & 5 deletions src/non_blocking/deflate/decode.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ use byteorder::ReadBytesExt;
use std::cmp;
use std::io;
use std::io::Read;
use std::ptr;

use deflate::symbol::{self, HuffmanCodec};
use lz77;
@@ -235,11 +234,11 @@ impl BlockDecoder {
}
fn truncate_old_buffer(&mut self) {
if self.buffer.len() > lz77::MAX_DISTANCE as usize * 4 {
let old_len = self.buffer.len();
let new_len = lz77::MAX_DISTANCE as usize;
unsafe {
let ptr = self.buffer.as_mut_ptr();
let src = ptr.add(self.buffer.len() - new_len);
ptr::copy_nonoverlapping(src, ptr, new_len);
{ // isolation to please borrow checker
let (dst, src) = self.buffer.split_at_mut(old_len - new_len);
dst[..new_len].copy_from_slice(src);
}
self.buffer.truncate(new_len);
self.offset = new_len;