From 8cf2380960d49f87bfcee9f44ba8dc4076d322be Mon Sep 17 00:00:00 2001 From: staphen Date: Fri, 9 May 2025 19:36:51 -0400 Subject: [PATCH] Fix ASAN alignment issues related to spawned monster deltas --- Source/msg.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Source/msg.cpp b/Source/msg.cpp index f462c4e4a..34dbb17e9 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -618,13 +618,13 @@ const std::byte *DeltaImportMonster(const std::byte *src, const std::byte *end, std::byte *DeltaExportSpawnedMonsters(std::byte *dst, const ankerl::unordered_dense::map &spawnedMonsters) { - auto &size = *reinterpret_cast(dst); - size = static_cast(spawnedMonsters.size()); + uint16_t size = SDL_SwapLE16(static_cast(spawnedMonsters.size())); + memcpy(dst, &size, sizeof(uint16_t)); dst += sizeof(uint16_t); for (const auto &deltaSpawnedMonster : spawnedMonsters) { - auto &monsterId = *reinterpret_cast(dst); - monsterId = static_cast(deltaSpawnedMonster.first); + uint16_t monsterId = SDL_SwapLE16(static_cast(deltaSpawnedMonster.first)); + memcpy(dst, &monsterId, sizeof(uint16_t)); dst += sizeof(uint16_t); memcpy(dst, &deltaSpawnedMonster.second, sizeof(DSpawnedMonster)); @@ -639,7 +639,10 @@ const std::byte *DeltaImportSpawnedMonsters(const std::byte *src, const std::byt if (src == nullptr || src + sizeof(uint16_t) > end) return nullptr; - uint16_t size = *reinterpret_cast(src); + uint16_t size; + memcpy(&size, src, sizeof(uint16_t)); + size = SDL_SwapLE16(size); + src += sizeof(uint16_t); if (size > MaxMonsters) return nullptr; @@ -647,11 +650,12 @@ const std::byte *DeltaImportSpawnedMonsters(const std::byte *src, const std::byt if (src + requiredBytes > end) return nullptr; - src += sizeof(uint16_t); - for (size_t i = 0; i < size; i++) { - uint16_t monsterId = *reinterpret_cast(src); + uint16_t monsterId; + memcpy(&monsterId, src, sizeof(uint16_t)); + monsterId = SDL_SwapLE16(monsterId); src += sizeof(uint16_t); + DSpawnedMonster spawnedMonster; memcpy(&spawnedMonster, src, sizeof(DSpawnedMonster)); src += sizeof(DSpawnedMonster);