about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-08-28 00:02:42 +0000
committerbors <bors@rust-lang.org>2023-08-28 00:02:42 +0000
commit1baf77aad0328c9a2fac290e604ee54d23c81a66 (patch)
tree33614ea99295fb32f954760d544d9138572574e3 /library/std/src
parent8550f15e148407159af401e02b1d9259762b3496 (diff)
parent1c6d867d78475fd8c6274e2b64ebb27735b6cabf (diff)
downloadrust-1baf77aad0328c9a2fac290e604ee54d23c81a66.tar.gz
rust-1baf77aad0328c9a2fac290e604ee54d23c81a66.zip
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.)
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/sys/unix/alloc.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/library/std/src/sys/unix/alloc.rs b/library/std/src/sys/unix/alloc.rs
index 8604b53983d..af0089978ec 100644
--- a/library/std/src/sys/unix/alloc.rs
+++ b/library/std/src/sys/unix/alloc.rs
@@ -86,7 +86,11 @@ cfg_if::cfg_if! {
     } else if #[cfg(target_os = "wasi")] {
         #[inline]
         unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
-            libc::aligned_alloc(layout.align(), layout.size()) as *mut u8
+            // C11 aligned_alloc requires that the size be a multiple of the alignment.
+            // Layout already checks that the size rounded up doesn't overflow isize::MAX.
+            let align = layout.align();
+            let size = layout.size().next_multiple_of(align);
+            libc::aligned_alloc(align, size) as *mut u8
         }
     } else {
         #[inline]