Skip to content

Commit a6c7f34

Browse files
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 <[email protected]>
1 parent ccb855d commit a6c7f34

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

shared/source/helpers/string.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ inline std::unique_ptr<T[]> makeCopy(const void *src, size_t size) {
115115
if (size == 0) {
116116
return nullptr;
117117
}
118-
using ElT = typename std::remove_all_extents<T>::type;
119-
std::unique_ptr<T[]> copiedData(new ElT[size]);
118+
119+
static_assert(sizeof(T) == 1u && std::is_trivially_copyable_v<T>);
120+
121+
auto copiedData = std::make_unique<T[]>(size);
120122
memcpy_s(copiedData.get(), size, src, size);
123+
121124
return copiedData;
122125
}

0 commit comments

Comments
 (0)