Skip to content

Commit 2eca806

Browse files
committed
Correct offset by c_void and realloc size
#51
1 parent 6be2aea commit 2eca806

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/ffi/libc.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ pub unsafe fn malloc(size: size_t) -> *mut c_void {
1616
*(p as *mut size_t) = size;
1717
p.offset(MALLOC_HEADER) as *mut c_void
1818
}
19+
1920
pub unsafe fn free(p: *mut c_void) {
20-
let p = p.offset(-MALLOC_HEADER) as *mut u8;
21+
let p = (p as *mut u8).offset(-MALLOC_HEADER);
2122
let size = *(p as *mut size_t);
2223
let lay = Layout::from_size_align_unchecked(MALLOC_HEADER as usize + size, MALLOC_ALIGN);
2324
alloc::dealloc(p, lay);
2425
}
25-
pub unsafe fn realloc(p: *mut c_void, _size: size_t) -> *mut c_void {
26-
let p = p.offset(-MALLOC_HEADER) as *mut u8;
26+
27+
pub unsafe fn realloc(p: *mut c_void, new_size: size_t) -> *mut c_void {
28+
let p = (p as *mut u8).offset(-MALLOC_HEADER);
2729
let size = *(p as *mut size_t);
2830
let lay = Layout::from_size_align_unchecked(MALLOC_HEADER as usize + size, MALLOC_ALIGN);
29-
let p = alloc::realloc(p, lay, size);
31+
let p = alloc::realloc(p, lay, new_size);
3032
if p.is_null() {
3133
return ptr::null_mut();
3234
}
33-
*(p as *mut size_t) = size;
35+
*(p as *mut size_t) = new_size;
3436
p.offset(MALLOC_HEADER) as *mut c_void
3537
}
3638

0 commit comments

Comments
 (0)