Skip to content

Commit

Permalink
ARROW-7836: [Rust] "allocate_aligned"/"reallocate" need to initialize…
Browse files Browse the repository at this point in the history
… memory to avoid UB

Closes #6422 from paddyhoran/init and squashes the following commits:

c90f153 <Paddy Horan> Addressed review comments.
6a6d0f4 <Paddy Horan> Initialize memory at allocation time.

Authored-by: Paddy Horan <paddyhoran@hotmail.com>
Signed-off-by: Paddy Horan <paddyhoran@hotmail.com>
  • Loading branch information
paddyhoran committed Feb 17, 2020
1 parent d6889f7 commit 7bd02c8
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions rust/arrow/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const ALIGNMENT: usize = 64;
pub fn allocate_aligned(size: usize) -> *mut u8 {
unsafe {
let layout = Layout::from_size_align_unchecked(size, ALIGNMENT);
std::alloc::alloc(layout)
std::alloc::alloc_zeroed(layout)
}
}

Expand All @@ -38,11 +38,15 @@ pub fn free_aligned(p: *mut u8, size: usize) {

pub fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 {
unsafe {
std::alloc::realloc(
let new_ptr = std::alloc::realloc(
ptr,
Layout::from_size_align_unchecked(old_size, ALIGNMENT),
new_size,
)
);
if !new_ptr.is_null() && new_size > old_size {
new_ptr.add(old_size).write_bytes(0, new_size - old_size);
}
new_ptr
}
}

Expand Down

0 comments on commit 7bd02c8

Please sign in to comment.