From a002d4b758bc4d5d889db1b85f689e480fd7957b Mon Sep 17 00:00:00 2001 From: ajz34 Date: Sun, 7 Jun 2026 14:02:05 +0800 Subject: [PATCH] rstsr-common: make statement of possible UB on aligned allocation, disable non-linux/macos aligned alloc --- rstsr-common/src/alloc_vec.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/rstsr-common/src/alloc_vec.rs b/rstsr-common/src/alloc_vec.rs index 130c8ae..e6785a2 100644 --- a/rstsr-common/src/alloc_vec.rs +++ b/rstsr-common/src/alloc_vec.rs @@ -15,11 +15,24 @@ use core::ptr::NonNull; /// This is not a very good function, since `set_len` on uninitialized memory is /// undefined-behavior (UB). /// Nevertheless, if `T` is some type of `MaybeUninit`, then this will not UB. +/// +/// # OS system dependent +/// +/// Current implementation of aligned allocation may be UB on Windows. We will disable aligned +/// allocation on platforms other than Linux and MacOS until we find a better solution. +/// +/// See also . pub unsafe fn uninitialized_vec(size: usize) -> Result> { - #[cfg(not(feature = "aligned_alloc"))] + #[cfg(all(not(target_os = "linux"), not(target_os = "macos")))] return unaligned_uninitialized_vec(size); - #[cfg(feature = "aligned_alloc")] - return aligned_uninitialized_vec::(size, 64); + + #[cfg(any(target_os = "linux", target_os = "macos"))] + { + #[cfg(not(feature = "aligned_alloc"))] + return unaligned_uninitialized_vec(size); + #[cfg(feature = "aligned_alloc")] + return aligned_uninitialized_vec::(size, 64); + } } /// Create an unaligned uninitialized vector with the given size. @@ -57,8 +70,6 @@ pub fn aligned_alloc(numbytes: usize, alignment: usize) -> Result