Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Fix insufficient alignment of malloc's return value on 32-bit
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 14, 2023
1 parent 389373f commit b8a0863
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@ mod externs {
use core::ptr;
use core::slice;

const HEADER: usize = mem::size_of::<usize>();
const HEADER: usize = {
let need_len = mem::size_of::<usize>();
// Round up to multiple of MALLOC_ALIGN.
(need_len + MALLOC_ALIGN - 1) & !(MALLOC_ALIGN - 1)
};

// `max_align_t` may be bigger than this, but libyaml does not use `long
// double` or u128.
const MALLOC_ALIGN: usize = mem::align_of::<usize>();
const MALLOC_ALIGN: usize = {
let int_align = mem::align_of::<libc::c_ulong>();
let ptr_align = mem::align_of::<usize>();
if int_align >= ptr_align {
int_align
} else {
ptr_align
}
};

pub unsafe fn malloc(size: libc::c_ulong) -> *mut libc::c_void {
let size = HEADER.force_add(size.force_into());
Expand Down

0 comments on commit b8a0863

Please sign in to comment.