diff --git a/CMakeLists.txt b/CMakeLists.txt index 60c4e3f8..ad5bd01b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,9 @@ project(Darkflame LANGUAGES C CXX ) -# TEMP - Sanitizer flags -if (UNIX AND NOT APPLE) - # add_compile_options("-fsanitize=address,undefined" "-fvisibility=default") - add_compile_options("-fsanitize=undefined" "-fvisibility=default") +# Sanitizer flags - TODO: Make CMake preset before finalizing PR +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") + add_compile_options("-fsanitize=undefined") add_link_options("-fsanitize=undefined") if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") @@ -285,7 +284,7 @@ if(MSVC) # add_compile_options("/W4") # Want to enable warnings eventually, but WAY too much noise right now elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") - add_compile_options("-Wuninitialized" "-Wold-style-cast" "-Wstrict-aliasing") + add_compile_options("-Wuninitialized" "-Wold-style-cast" "-Wstrict-aliasing=01") else() message(WARNING "Unknown compiler: '${CMAKE_CXX_COMPILER_ID}' - No warning flags enabled.") endif() diff --git a/cmake/toolchains/linux-clang.cmake b/cmake/toolchains/linux-clang.cmake index b4578a38..1e1ed5c3 100644 --- a/cmake/toolchains/linux-clang.cmake +++ b/cmake/toolchains/linux-clang.cmake @@ -1,6 +1,7 @@ # Try and find a clang-16 install, falling back to a generic clang install otherwise find_program(CLANG_C_COMPILER clang-16 | clang REQUIRED) find_program(CLANG_CXX_COMPILER clang++-16 | clang++ REQUIRED) +find_program(CLANG_CXX_LINKER lld REQUIRED) # Debug messages message(DEBUG "CLANG_C_COMPILER = ${CLANG_C_COMPILER}") diff --git a/dCommon/GeneralUtils.cpp b/dCommon/GeneralUtils.cpp index e254a23a..cc4c4b1e 100644 --- a/dCommon/GeneralUtils.cpp +++ b/dCommon/GeneralUtils.cpp @@ -53,9 +53,9 @@ bool static _IsSuffixChar(const uint8_t c) { bool GeneralUtils::details::_NextUTF8Char(std::string_view& slice, uint32_t& out) { const size_t rem = slice.length(); if (slice.empty()) return false; - const auto* bytes = &slice.front(); + const char* const bytes = slice.data(); if (rem > 0) { - const uint8_t first = bytes[0]; + const uint8_t first = static_cast(bytes[0]); if (first < 0x80) { // 1 byte character out = static_cast(first & 0x7F); slice.remove_prefix(1); @@ -64,7 +64,7 @@ bool GeneralUtils::details::_NextUTF8Char(std::string_view& slice, uint32_t& out // middle byte, not valid at start, fall through } else if (first < 0xE0) { // two byte character if (rem > 1) { - const uint8_t second = bytes[1]; + const uint8_t second = static_cast(bytes[1]); if (_IsSuffixChar(second)) { out = (static_cast(first & 0x1F) << 6) + static_cast(second & 0x3F); @@ -74,8 +74,8 @@ bool GeneralUtils::details::_NextUTF8Char(std::string_view& slice, uint32_t& out } } else if (first < 0xF0) { // three byte character if (rem > 2) { - const uint8_t second = bytes[1]; - const uint8_t third = bytes[2]; + const uint8_t second = static_cast(bytes[1]); + const uint8_t third = static_cast(bytes[2]); if (_IsSuffixChar(second) && _IsSuffixChar(third)) { out = (static_cast(first & 0x0F) << 12) + (static_cast(second & 0x3F) << 6) @@ -86,9 +86,9 @@ bool GeneralUtils::details::_NextUTF8Char(std::string_view& slice, uint32_t& out } } else if (first < 0xF8) { // four byte character if (rem > 3) { - const uint8_t second = bytes[1]; - const uint8_t third = bytes[2]; - const uint8_t fourth = bytes[3]; + const uint8_t second = static_cast(bytes[1]); + const uint8_t third = static_cast(bytes[2]); + const uint8_t fourth = static_cast(bytes[3]); if (_IsSuffixChar(second) && _IsSuffixChar(third) && _IsSuffixChar(fourth)) { out = (static_cast(first & 0x07) << 18) + (static_cast(second & 0x3F) << 12) diff --git a/dWorldServer/WorldServer.cpp b/dWorldServer/WorldServer.cpp index 44265506..84a0c1a9 100644 --- a/dWorldServer/WorldServer.cpp +++ b/dWorldServer/WorldServer.cpp @@ -1396,7 +1396,7 @@ void HandlePacket(Packet* packet) { } default: - // Need to use memcpy instead of reinterpret_cast to avoid UB + // Need to use memcpy instead of reinterpret_cast to avoid misaligned reads, which are UB auto messageId = MessageType::World::INVALID; std::memcpy(&messageId, &packet->data[3], sizeof(MessageType::World));