Improve safety of makeCopy() function

The mentioned function allocates and copies elements
of size 1. Therefore, the implementation was simplified
and additional check was added to avoid possible UBs.

Signed-off-by: Wrobel, Patryk <patryk.wrobel@intel.com>
This commit is contained in:
Wrobel, Patryk
2022-08-22 11:24:41 +00:00
committed by Compute-Runtime-Automation
parent ccb855de32
commit a6c7f341dd

View File

@@ -115,8 +115,11 @@ inline std::unique_ptr<T[]> makeCopy(const void *src, size_t size) {
if (size == 0) {
return nullptr;
}
using ElT = typename std::remove_all_extents<T>::type;
std::unique_ptr<T[]> copiedData(new ElT[size]);
static_assert(sizeof(T) == 1u && std::is_trivially_copyable_v<T>);
auto copiedData = std::make_unique<T[]>(size);
memcpy_s(copiedData.get(), size, src, size);
return copiedData;
}