Skip to content

Commit 8e5fd21

Browse files
authored
Merge pull request #138 from cristian64/fortify_source_warnings
Address `-Wfortify-source` warnings.
2 parents 4c94370 + 11e56d6 commit 8e5fd21

2 files changed

Lines changed: 19 additions & 29 deletions

File tree

‎Source/CMakeLists.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ endif ()
7575

7676
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
7777
add_compile_options(
78-
-Wno-fortify-source # FIXME(CA): Too verbose at the moment, but worth fixing
7978
-Wno-sign-conversion # FIXME(CA): Too verbose at the moment, but worth fixing
8079
)
8180
endif ()

‎Source/MemoryScanner/MemoryScanner.h‎

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,54 +75,45 @@ class MemScanner
7575
T secondByte;
7676
std::memcpy(&firstByte, first, sizeof(T));
7777
std::memcpy(&secondByte, second, sizeof(T));
78-
size_t size = sizeof(T);
79-
switch (size)
80-
{
81-
case 2:
78+
79+
constexpr size_t size{sizeof(T)};
80+
static_assert(size == 1 || size == 2 || size == 4 || size == 8, "Unexpected type size");
81+
82+
if constexpr (size == 2)
8283
{
83-
u16 firstHalfword = 0;
84-
std::memcpy(&firstHalfword, &firstByte, sizeof(u16));
84+
u16 firstHalfword = Common::bit_cast<u16, T>(firstByte);
8585
firstHalfword = Common::bSwap16(firstHalfword);
86-
std::memcpy(&firstByte, &firstHalfword, sizeof(u16));
86+
firstByte = Common::bit_cast<T, u16>(firstHalfword);
8787
if (bswapSecond)
8888
{
89-
std::memcpy(&firstHalfword, &secondByte, sizeof(u16));
89+
firstHalfword = Common::bit_cast<u16, T>(secondByte);
9090
firstHalfword = Common::bSwap16(firstHalfword);
91-
std::memcpy(&secondByte, &firstHalfword, sizeof(u16));
91+
secondByte = Common::bit_cast<T, u16>(firstHalfword);
9292
}
93-
break;
9493
}
95-
case 4:
94+
else if constexpr (size == 4)
9695
{
97-
u32 firstWord = 0;
98-
std::memcpy(&firstWord, &firstByte, sizeof(u32));
96+
u32 firstWord = Common::bit_cast<u32, T>(firstByte);
9997
firstWord = Common::bSwap32(firstWord);
100-
std::memcpy(&firstByte, &firstWord, sizeof(u32));
98+
firstByte = Common::bit_cast<T, u32>(firstWord);
10199
if (bswapSecond)
102100
{
103-
std::memcpy(&firstWord, &secondByte, sizeof(u32));
101+
firstWord = Common::bit_cast<u32, T>(secondByte);
104102
firstWord = Common::bSwap32(firstWord);
105-
std::memcpy(&secondByte, &firstWord, sizeof(u32));
103+
secondByte = Common::bit_cast<T, u32>(firstWord);
106104
}
107-
break;
108105
}
109-
case 8:
106+
else if constexpr (size == 8)
110107
{
111-
u64 firstDoubleword = 0;
112-
std::memcpy(&firstDoubleword, &firstByte, sizeof(u64));
108+
u64 firstDoubleword = Common::bit_cast<u64, T>(firstByte);
113109
firstDoubleword = Common::bSwap64(firstDoubleword);
114-
std::memcpy(&firstByte, &firstDoubleword, sizeof(u64));
110+
firstByte = Common::bit_cast<T, u64>(firstDoubleword);
115111
if (bswapSecond)
116112
{
117-
std::memcpy(&firstDoubleword, &secondByte, sizeof(u64));
113+
firstDoubleword = Common::bit_cast<u64, T>(secondByte);
118114
firstDoubleword = Common::bSwap64(firstDoubleword);
119-
std::memcpy(&secondByte, &firstDoubleword, sizeof(u64));
115+
secondByte = Common::bit_cast<T, u64>(firstDoubleword);
120116
}
121-
break;
122-
}
123-
default:
124-
assert(0 && "Unexpected type size");
125-
break;
126117
}
127118

128119
if constexpr (std::is_floating_point<T>::value)

0 commit comments

Comments
 (0)