Skip to content

Commit

Permalink
Add assertions to ensure memcpySpan and memsetSpan are used on trivia…
Browse files Browse the repository at this point in the history
…lly copyable spans

https://bugs.webkit.org/show_bug.cgi?id=264619

Reviewed by Darin Adler.

Bad things are likely to happen if used on objects with custom
constructors or destructors or assignment operators, because they won't
be executed. Add safety checks for this.

* Source/WTF/wtf/Algorithms.h:
(WTF::memcpySpan):
(WTF::memsetSpan):

Canonical link: https://commits.webkit.org/270796@main
  • Loading branch information
mcatanzaro committed Nov 16, 2023
1 parent 21083b8 commit 3ae0cd0
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Source/WTF/wtf/Algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <cstring>
#include <span>
#include <type_traits>
#include <wtf/Assertions.h>

namespace WTF {
Expand Down Expand Up @@ -71,12 +72,15 @@ void memcpySpan(std::span<T> destination, std::span<U> source)
{
RELEASE_ASSERT(destination.size() == source.size());
static_assert(sizeof(T) == sizeof(U));
static_assert(std::is_trivially_copyable_v<T>);
static_assert(std::is_trivially_copyable_v<U>);
memcpy(destination.data(), source.data(), destination.size() * sizeof(T));
}

template<typename T>
void memsetSpan(std::span<T> destination, uint8_t byte)
{
static_assert(std::is_trivially_copyable_v<T>);
memset(destination.data(), byte, destination.size() * sizeof(T));
}

Expand Down

0 comments on commit 3ae0cd0

Please sign in to comment.