Skip to content

Commit

Permalink
Correct and unify invalid parameters processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr-Konovalov committed Apr 3, 2024
1 parent a746629 commit 687a277
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 7 additions & 9 deletions include/pstl_offload/internal/usm_memory_replacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -322,20 +322,18 @@ 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);
}

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);
Expand Down
2 changes: 2 additions & 0 deletions src/pstl_offload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ __internal_msize(void* __user_ptr)
{
#if _WIN64
errno = EINVAL;
_invalid_parameter_noinfo();
return -1;
#else
return 0;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 687a277

Please sign in to comment.