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

The scope of the unsafe block can be appropriately reduced #273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions capnp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ impl Word {
let mut result: Vec<Word> = Vec::with_capacity(length);
unsafe {
result.set_len(length);
let p: *mut u8 = result.as_mut_ptr() as *mut u8;
core::ptr::write_bytes(p, 0u8, length * core::mem::size_of::<Word>());
}
let p: *mut u8 = result.as_mut_ptr() as *mut u8;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indentation is wrong here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #305

unsafe { core::ptr::write_bytes(p, 0u8, length * core::mem::size_of::<Word>()) };
result
}

Expand Down
22 changes: 11 additions & 11 deletions capnp/src/private/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2727,16 +2727,16 @@ impl <'a> PointerBuilder<'a> {
wire_helpers::set_struct_pointer(
self.arena,
self.segment_id, self.cap_table, self.pointer, *value, canonicalize)?;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

Ok(())
}
}

pub fn set_list(&self, value: &ListReader, canonicalize: bool) -> Result<()> {
unsafe {
wire_helpers::set_list_pointer(self.arena, self.segment_id,
self.cap_table, self.pointer, *value, canonicalize)?;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

Ok(())
}
}

pub fn set_text(&self, value: &str) {
Expand Down Expand Up @@ -3102,33 +3102,33 @@ impl <'a> StructBuilder<'a> {
return Ok(())
}

unsafe {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove this unsafe block then the following lines should be less indented.

if self.data_size > shared_data_size {
// Since the target is larger than the source, make sure to zero out the extra bits that the
// source doesn't have.
if self.data_size == 1 {
self.set_bool_field(0, false);
} else {
let unshared = self.data.offset((shared_data_size / BITS_PER_BYTE as u32) as isize);
ptr::write_bytes(unshared, 0, ((self.data_size - shared_data_size) / BITS_PER_BYTE as u32) as usize);
let unshared = unsafe { self.data.offset((shared_data_size / BITS_PER_BYTE as u32) as isize) };
unsafe { ptr::write_bytes(unshared, 0, ((self.data_size - shared_data_size) / BITS_PER_BYTE as u32) as usize) };
}
}

// Copy over the shared part.
if shared_data_size == 1 {
self.set_bool_field(0, other.get_bool_field(0));
} else {
ptr::copy_nonoverlapping(other.data, self.data, (shared_data_size / BITS_PER_BYTE as u32) as usize);
unsafe { ptr::copy_nonoverlapping(other.data, self.data, (shared_data_size / BITS_PER_BYTE as u32) as usize) };
}

// Zero out all pointers in the target.
for i in 0..self.pointer_count as isize {
wire_helpers::zero_object(self.arena, self.segment_id, self.pointers.offset(i) as *mut _);
unsafe { wire_helpers::zero_object(self.arena, self.segment_id, self.pointers.offset(i) as *mut _) };
}
ptr::write_bytes(self.pointers, 0u8, self.pointer_count as usize);
unsafe { ptr::write_bytes(self.pointers, 0u8, self.pointer_count as usize) };

for i in 0..shared_pointer_count as isize {
wire_helpers::copy_pointer(self.arena,
unsafe { wire_helpers::copy_pointer(self.arena,
self.segment_id,
self.cap_table,
self.pointers.offset(i),
Expand All @@ -3137,9 +3137,9 @@ impl <'a> StructBuilder<'a> {
other.cap_table,
other.pointers.offset(i),
other.nesting_limit,
false)?;
false)? };
}
}


Ok(())
}
Expand Down
32 changes: 16 additions & 16 deletions capnp/src/serialize_packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ impl <R> Read for PackedRead<R> where R: BufRead {

assert!(len % 8 == 0, "PackedRead reads must be word-aligned.");

let mut out = out_buf.as_mut_ptr();
let out_end: *mut u8 = out_buf.as_mut_ptr().wrapping_offset(len as isize);

let (mut in_ptr, mut in_end) = self.get_read_buffer()?;
let mut buffer_begin = in_ptr;
let mut size = ptr_sub(in_end, in_ptr);
if size == 0 {
return Ok(0);
}

unsafe {
let mut out = out_buf.as_mut_ptr();
let out_end: *mut u8 = out_buf.as_mut_ptr().wrapping_offset(len as isize);

let (mut in_ptr, mut in_end) = self.get_read_buffer()?;
let mut buffer_begin = in_ptr;
let mut size = ptr_sub(in_end, in_ptr);
if size == 0 {
return Ok(0);
}

loop {
let tag: u8;

Expand Down Expand Up @@ -222,13 +222,13 @@ struct PackedWrite<W> where W: Write {

impl <W> Write for PackedWrite<W> where W: Write {
fn write_all(&mut self, in_buf: &[u8]) -> Result<()> {
let mut buf_idx: usize = 0;
let mut buf: [u8; 64] = [0; 64];

let mut in_ptr: *const u8 = in_buf.as_ptr();
let in_end: *const u8 = in_buf.as_ptr().wrapping_offset(in_buf.len() as isize);

unsafe {
let mut buf_idx: usize = 0;
let mut buf: [u8; 64] = [0; 64];

let mut in_ptr: *const u8 = in_buf.as_ptr();
let in_end: *const u8 = in_buf.as_ptr().wrapping_offset(in_buf.len() as isize);

while in_ptr < in_end {

if buf_idx + 10 > buf.len() {
Expand Down