Skip to content

Commit 1baf77a

Browse files
committed
Auto merge of #115254 - cuviper:aligned_alloc-size, r=thomcc
wasi: round up the size for `aligned_alloc` C11 `aligned_alloc` requires that the size be a multiple of the alignment. This is enforced in the wasi-libc emmalloc implementation, which always returns NULL if the size is not a multiple. (The default `MALLOC_IMPL=dlmalloc` does not currently check this.)
2 parents 8550f15 + 1c6d867 commit 1baf77a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

library/std/src/sys/unix/alloc.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ cfg_if::cfg_if! {
8686
} else if #[cfg(target_os = "wasi")] {
8787
#[inline]
8888
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
89-
libc::aligned_alloc(layout.align(), layout.size()) as *mut u8
89+
// C11 aligned_alloc requires that the size be a multiple of the alignment.
90+
// Layout already checks that the size rounded up doesn't overflow isize::MAX.
91+
let align = layout.align();
92+
let size = layout.size().next_multiple_of(align);
93+
libc::aligned_alloc(align, size) as *mut u8
9094
}
9195
} else {
9296
#[inline]

0 commit comments

Comments
 (0)