diff --git a/libcxx/include/__memory/shared_ptr.h b/libcxx/include/__memory/shared_ptr.h index dce44a7b3732..a5bae83e0b61 100644 --- a/libcxx/include/__memory/shared_ptr.h +++ b/libcxx/include/__memory/shared_ptr.h @@ -1134,7 +1134,8 @@ struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count __alloc_.~_Alloc(); size_t __size = __unbounded_array_control_block::__bytes_for(__count_); _AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this); - allocator_traits<_StorageAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*__storage), __size); + allocator_traits<_StorageAlloc>::deallocate( + __tmp, _PointerTraits::pointer_to(*__storage), __size / sizeof(_AlignedStorage)); } _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; @@ -1217,7 +1218,7 @@ struct __bounded_array_control_block<_Tp[_Count], _Alloc> _ControlBlockAlloc __tmp(__alloc_); __alloc_.~_Alloc(); - allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), sizeof(*this)); + allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), 1); } _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; diff --git a/libcxx/test/libcxx/memory/shared_ptr_array.pass.cpp b/libcxx/test/libcxx/memory/shared_ptr_array.pass.cpp new file mode 100644 index 000000000000..772198304b41 --- /dev/null +++ b/libcxx/test/libcxx/memory/shared_ptr_array.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// REQUIRES: -fsized-deallocation +// ADDITIONAL_COMPILE_FLAGS: -fsized-deallocation + +// This test will fail with ASan if the implementation passes different sizes +// to corresponding allocation and deallocation functions. + +#include + +int main(int, char**) { + std::allocate_shared(std::allocator{}, 10); + std::make_shared(10); + + std::allocate_shared(std::allocator{}); + std::make_shared(); + + return 0; +}