Skip to content

Commit

Permalink
unsafe improvements (#6551)
Browse files Browse the repository at this point in the history
* Remove unnecessary use of `unsafe` by reusing existing code

* Remove unnecessary use of MaybeUninit
  • Loading branch information
ssbr authored Oct 21, 2024
1 parent 18ef662 commit 04d0ebe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions arrow-buffer/src/builder/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ impl<O: ArrowNativeType> OffsetBufferBuilder<O> {
///
/// Panics if offsets overflow `O`
pub fn finish_cloned(&self) -> OffsetBuffer<O> {
O::from_usize(self.last_offset).expect("overflow");
unsafe { OffsetBuffer::new_unchecked(self.offsets.clone().into()) }
let cloned = Self {
offsets: self.offsets.clone(),
last_offset: self.last_offset,
};
cloned.finish()
}
}

Expand Down
8 changes: 4 additions & 4 deletions arrow-buffer/src/util/bit_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ unsafe fn set_upto_64bits(
}

/// # Safety
/// The caller must ensure all arguments are within the valid range.
/// The caller must ensure `data` has `offset..(offset + 8)` range, and `count <= 8`.
#[inline]
unsafe fn read_bytes_to_u64(data: &[u8], offset: usize, count: usize) -> u64 {
debug_assert!(count <= 8);
let mut tmp = std::mem::MaybeUninit::<u64>::new(0);
let mut tmp: u64 = 0;
let src = data.as_ptr().add(offset);
unsafe {
std::ptr::copy_nonoverlapping(src, tmp.as_mut_ptr() as *mut u8, count);
tmp.assume_init()
std::ptr::copy_nonoverlapping(src, &mut tmp as *mut _ as *mut u8, count);
}
tmp
}

/// # Safety
Expand Down

0 comments on commit 04d0ebe

Please sign in to comment.