소스 검색

file_util: Add static assertions to ReadBytes() and WriteBytes()

Ensure that the actual types being passed in are trivially copyable. The internal
call to ReadArray() and WriteArray() will always succeed, since they're passed a pointer to char*
which is always trivially copyable.
Lioncash 8 년 전
부모
커밋
40d2dcabd7
1개의 변경된 파일6개의 추가작업 그리고 2개의 파일을 삭제
  1. 6 2
      src/common/file_util.h

+ 6 - 2
src/common/file_util.h

@@ -202,11 +202,15 @@ public:
         return items_written;
     }
 
-    size_t ReadBytes(void* data, size_t length) {
+    template <typename T>
+    size_t ReadBytes(T* data, size_t length) {
+        static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable");
         return ReadArray(reinterpret_cast<char*>(data), length);
     }
 
-    size_t WriteBytes(const void* data, size_t length) {
+    template <typename T>
+    size_t WriteBytes(const T* data, size_t length) {
+        static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable");
         return WriteArray(reinterpret_cast<const char*>(data), length);
     }