diff options
| author | 2018-07-23 14:52:32 -0400 | |
|---|---|---|
| committer | 2018-07-23 14:53:54 -0400 | |
| commit | 1432912ae88e750ae0aae8972d7e260538d271df (patch) | |
| tree | adb2360ca68a60f3ec03d1902d6e33ff09b27f18 | |
| parent | vi: std::move std::vector in constructors where applicable (diff) | |
| download | yuzu-1432912ae88e750ae0aae8972d7e260538d271df.tar.gz yuzu-1432912ae88e750ae0aae8972d7e260538d271df.tar.xz yuzu-1432912ae88e750ae0aae8972d7e260538d271df.zip | |
vi: Add std::is_trivially_copyable checks to Read and Write functions
It's undefined behavior to memcpy an object that isn't considered
trivially copyable, so put a compile-time check in to make sure this
doesn't occur.
| -rw-r--r-- | src/core/hle/service/vi/vi.cpp | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/core/hle/service/vi/vi.cpp b/src/core/hle/service/vi/vi.cpp index 049957503..993f1e65a 100644 --- a/src/core/hle/service/vi/vi.cpp +++ b/src/core/hle/service/vi/vi.cpp | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <array> | 6 | #include <array> |
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include <type_traits> | ||
| 8 | #include <utility> | 9 | #include <utility> |
| 9 | #include <boost/optional.hpp> | 10 | #include <boost/optional.hpp> |
| 10 | #include "common/alignment.h" | 11 | #include "common/alignment.h" |
| @@ -44,7 +45,9 @@ public: | |||
| 44 | 45 | ||
| 45 | template <typename T> | 46 | template <typename T> |
| 46 | T Read() { | 47 | T Read() { |
| 48 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable."); | ||
| 47 | ASSERT(read_index + sizeof(T) <= buffer.size()); | 49 | ASSERT(read_index + sizeof(T) <= buffer.size()); |
| 50 | |||
| 48 | T val; | 51 | T val; |
| 49 | std::memcpy(&val, buffer.data() + read_index, sizeof(T)); | 52 | std::memcpy(&val, buffer.data() + read_index, sizeof(T)); |
| 50 | read_index += sizeof(T); | 53 | read_index += sizeof(T); |
| @@ -54,7 +57,9 @@ public: | |||
| 54 | 57 | ||
| 55 | template <typename T> | 58 | template <typename T> |
| 56 | T ReadUnaligned() { | 59 | T ReadUnaligned() { |
| 60 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable."); | ||
| 57 | ASSERT(read_index + sizeof(T) <= buffer.size()); | 61 | ASSERT(read_index + sizeof(T) <= buffer.size()); |
| 62 | |||
| 58 | T val; | 63 | T val; |
| 59 | std::memcpy(&val, buffer.data() + read_index, sizeof(T)); | 64 | std::memcpy(&val, buffer.data() + read_index, sizeof(T)); |
| 60 | read_index += sizeof(T); | 65 | read_index += sizeof(T); |
| @@ -88,8 +93,12 @@ public: | |||
| 88 | 93 | ||
| 89 | template <typename T> | 94 | template <typename T> |
| 90 | void Write(const T& val) { | 95 | void Write(const T& val) { |
| 91 | if (buffer.size() < write_index + sizeof(T)) | 96 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable."); |
| 97 | |||
| 98 | if (buffer.size() < write_index + sizeof(T)) { | ||
| 92 | buffer.resize(buffer.size() + sizeof(T) + DefaultBufferSize); | 99 | buffer.resize(buffer.size() + sizeof(T) + DefaultBufferSize); |
| 100 | } | ||
| 101 | |||
| 93 | std::memcpy(buffer.data() + write_index, &val, sizeof(T)); | 102 | std::memcpy(buffer.data() + write_index, &val, sizeof(T)); |
| 94 | write_index += sizeof(T); | 103 | write_index += sizeof(T); |
| 95 | write_index = Common::AlignUp(write_index, 4); | 104 | write_index = Common::AlignUp(write_index, 4); |
| @@ -97,7 +106,9 @@ public: | |||
| 97 | 106 | ||
| 98 | template <typename T> | 107 | template <typename T> |
| 99 | void WriteObject(const T& val) { | 108 | void WriteObject(const T& val) { |
| 100 | u32_le size = static_cast<u32>(sizeof(val)); | 109 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable."); |
| 110 | |||
| 111 | const u32_le size = static_cast<u32>(sizeof(val)); | ||
| 101 | Write(size); | 112 | Write(size); |
| 102 | // TODO(Subv): Support file descriptors. | 113 | // TODO(Subv): Support file descriptors. |
| 103 | Write<u32_le>(0); // Fd count. | 114 | Write<u32_le>(0); // Fd count. |