reinterpret_cast-based type-punning is almost always UB

This commit is contained in:
jadebenn 2024-11-23 02:29:11 -08:00
parent 8eb3488812
commit 7740bbbaab
5 changed files with 14 additions and 11 deletions

View File

@ -285,7 +285,7 @@ if(MSVC)
# add_compile_options("/W4") # add_compile_options("/W4")
# Want to enable warnings eventually, but WAY too much noise right now # Want to enable warnings eventually, but WAY too much noise right now
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
add_compile_options("-Wuninitialized" "-Wold-style-cast") add_compile_options("-Wuninitialized" "-Wold-style-cast" "-Wstrict-aliasing")
else() else()
message(WARNING "Unknown compiler: '${CMAKE_CXX_COMPILER_ID}' - No warning flags enabled.") message(WARNING "Unknown compiler: '${CMAKE_CXX_COMPILER_ID}' - No warning flags enabled.")
endif() endif()

View File

@ -159,8 +159,7 @@ void RakNet::BitStream::Write<AMFIntValue&>(AMFIntValue& value) {
// Writes an AMFDoubleValue to BitStream // Writes an AMFDoubleValue to BitStream
template<> template<>
void RakNet::BitStream::Write<AMFDoubleValue&>(AMFDoubleValue& value) { void RakNet::BitStream::Write<AMFDoubleValue&>(AMFDoubleValue& value) {
double d = value.GetValue(); WriteAMFU64(*this, std::bit_cast<uint64_t>(value.GetValue()));
WriteAMFU64(*this, *reinterpret_cast<uint64_t*>(&d));
} }
// Writes an AMFStringValue to BitStream // Writes an AMFStringValue to BitStream

View File

@ -146,7 +146,7 @@ void WriteSd0Magic(char* input, uint32_t chunkSize) {
input[2] = '0'; input[2] = '0';
input[3] = 0x01; input[3] = 0x01;
input[4] = 0xFF; input[4] = 0xFF;
*reinterpret_cast<uint32_t*>(input + 5) = chunkSize; // Write the integer to the character array std::memcpy(&input[5], &chunkSize, sizeof(uint32_t)); // Write the integer to the character array
} }
bool CheckSd0Magic(std::istream& streamToCheck) { bool CheckSd0Magic(std::istream& streamToCheck) {

View File

@ -53,7 +53,7 @@ bool static _IsSuffixChar(const uint8_t c) {
bool GeneralUtils::details::_NextUTF8Char(std::string_view& slice, uint32_t& out) { bool GeneralUtils::details::_NextUTF8Char(std::string_view& slice, uint32_t& out) {
const size_t rem = slice.length(); const size_t rem = slice.length();
if (slice.empty()) return false; if (slice.empty()) return false;
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&slice.front()); const auto* bytes = &slice.front();
if (rem > 0) { if (rem > 0) {
const uint8_t first = bytes[0]; const uint8_t first = bytes[0];
if (first < 0x80) { // 1 byte character if (first < 0x80) { // 1 byte character

View File

@ -50,9 +50,9 @@ void NpcAgCourseStarter::OnMessageBoxResponse(Entity* self, Entity* sender, int3
if (data->values[1] != 0) return; if (data->values[1] != 0) return;
time_t startTime = std::time(0) + 4; // Offset for starting timer const time_t startTime = std::time(0) + 4; // Offset for starting timer
data->values[1] = *reinterpret_cast<float*>(&startTime); std::memcpy(&data->values[1], &startTime, sizeof(float));
Game::entityManager->SerializeEntity(self); Game::entityManager->SerializeEntity(self);
} else if (identifier == u"FootRaceCancel") { } else if (identifier == u"FootRaceCancel") {
@ -80,10 +80,14 @@ void NpcAgCourseStarter::OnFireEventServerSide(Entity* self, Entity* sender, std
LWOOBJID_EMPTY, "", sender->GetSystemAddress()); LWOOBJID_EMPTY, "", sender->GetSystemAddress());
scriptedActivityComponent->RemoveActivityPlayerData(sender->GetObjectID()); scriptedActivityComponent->RemoveActivityPlayerData(sender->GetObjectID());
} else if (args == "course_finish") { } else if (args == "course_finish") {
time_t endTime = std::time(0); const time_t endTime = std::time(0);
time_t finish = (endTime - *reinterpret_cast<time_t*>(&data->values[1]));
// Using memcpy since misaligned reads are UB
data->values[2] = *reinterpret_cast<float*>(&finish); time_t startTime{};
std::memcpy(&startTime, &data->values[1], sizeof(time_t));
const time_t finish = (endTime - startTime);
std::memcpy(&data->values[2], &finish, sizeof(float));
auto* missionComponent = sender->GetComponent<MissionComponent>(); auto* missionComponent = sender->GetComponent<MissionComponent>();
if (missionComponent != nullptr) { if (missionComponent != nullptr) {