From 687a277694d7c3893f79769efc47a8a77af9769d Mon Sep 17 00:00:00 2001 From: Alexandr Konovalov Date: Wed, 3 Apr 2024 15:39:50 +0200 Subject: [PATCH] Correct and unify invalid parameters processing. --- .../internal/usm_memory_replacement.h | 16 +++++++--------- src/pstl_offload.cpp | 2 ++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/pstl_offload/internal/usm_memory_replacement.h b/include/pstl_offload/internal/usm_memory_replacement.h index 62e63624377..3b7251f1c3c 100644 --- a/include/pstl_offload/internal/usm_memory_replacement.h +++ b/include/pstl_offload/internal/usm_memory_replacement.h @@ -141,12 +141,12 @@ __internal_aligned_alloc(std::size_t __size, std::size_t __alignment) #if _WIN64 // Under Windows, memory with explicitly set alignment must not be released by free() function, // but rather with _aligned_free(), so have to use malloc() for non-extended alignment allocations. - __res = __not_use_explicit_alignment == __alignment ? __original_malloc(__size) - : __original_aligned_alloc(__alignment, __size); + __res = (__not_use_explicit_alignment == __alignment) ? __original_malloc(__size) + : __original_aligned_alloc(__alignment, __size); #else // can always use aligned allocation, not interop issue with free() - __res = __original_aligned_alloc(__size, __not_use_explicit_alignment == __alignment ? alignof(std::max_align_t) - : __alignment); + __res = __original_aligned_alloc(__size, (__not_use_explicit_alignment == __alignment) ? alignof(std::max_align_t) + : __alignment); #endif } @@ -322,9 +322,8 @@ inline void* __attribute__((always_inline)) __libc_realloc(void *__ptr, std::siz inline void* __attribute__((always_inline)) _aligned_malloc(std::size_t __size, std::size_t __alignment) { // _aligned_malloc should reject zero or not power of two alignments - if (!::__pstl_offload::__is_power_of_two(__alignment)) + if (!::__pstl_offload::__verify_aligned_new_param(__alignment)) { - errno = EINVAL; return nullptr; } return ::__pstl_offload::__errno_handling_internal_aligned_alloc(__size, __alignment); @@ -332,10 +331,9 @@ inline void* __attribute__((always_inline)) _aligned_malloc(std::size_t __size, inline void* __attribute__((always_inline)) _aligned_realloc(void* __ptr, std::size_t __size, std::size_t __alignment) { - // _aligned_realloc should reject zero or not power of two alignments - if (!::__pstl_offload::__is_power_of_two(__alignment)) + // _aligned_realloc should reject zero or not power of two alignments, but not when it calls _aligned_free + if (__size && !::__pstl_offload::__verify_aligned_new_param(__alignment)) { - errno = EINVAL; return nullptr; } return ::__pstl_offload::__internal_aligned_realloc(__ptr, __size, __alignment); diff --git a/src/pstl_offload.cpp b/src/pstl_offload.cpp index 5b5f6c2fa6b..7f24b84f563 100644 --- a/src/pstl_offload.cpp +++ b/src/pstl_offload.cpp @@ -193,6 +193,7 @@ __internal_msize(void* __user_ptr) { #if _WIN64 errno = EINVAL; + _invalid_parameter_noinfo(); return -1; #else return 0; @@ -221,6 +222,7 @@ __internal_aligned_msize(void* __user_ptr, std::size_t __alignment, std::size_t if (__user_ptr == nullptr || !__is_power_of_two(__alignment)) { errno = EINVAL; + _invalid_parameter_noinfo(); return -1; }