From ea06a13429c94b2d17f7b3a82a01bd7b823ee51f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 12 Nov 2022 20:04:21 +0100 Subject: [PATCH] Fix aliasing warnings in `CGameContext::OnMapChange` Fix warnings with `-fstrict-aliasing` and `-Wstrict-aliasing=2` by not casting the map reader data to `int *`: ``` src/game/server/gamecontext.cpp: In member function 'virtual void CGameContext::OnMapChange(char*, int)': src/game/server/gamecontext.cpp:3623:56: warning: dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing] 3623 | pData = (int *)&MapInfo; | ^~~~~~~~ src/game/server/gamecontext.cpp:3631:48: warning: dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing] 3631 | pData = (int *)&MapInfo; | ^~~~~~~~ ``` As the map reader data is return as `void *` and never used as `int *`, the redundant cast is removed to fix the warnings. Also improve readability by moving `pInfo` variable declaration. --- src/game/server/gamecontext.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 5e40c8478..8571e4be3 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -3594,15 +3594,15 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) { int TypeID; int ItemID; - int *pData = (int *)Reader.GetItem(i, &TypeID, &ItemID); + void *pData = Reader.GetItem(i, &TypeID, &ItemID); int Size = Reader.GetItemSize(i); CMapItemInfoSettings MapInfo; if(TypeID == MAPITEMTYPE_INFO && ItemID == 0) { FoundInfo = true; - CMapItemInfoSettings *pInfo = (CMapItemInfoSettings *)pData; if(Size >= (int)sizeof(CMapItemInfoSettings)) { + CMapItemInfoSettings *pInfo = (CMapItemInfoSettings *)pData; if(pInfo->m_Settings > -1) { SettingsIndex = pInfo->m_Settings; @@ -3620,15 +3620,15 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) { MapInfo = *pInfo; MapInfo.m_Settings = SettingsIndex; - pData = (int *)&MapInfo; + pData = &MapInfo; Size = sizeof(MapInfo); } } else { - *(CMapItemInfo *)&MapInfo = *(CMapItemInfo *)pInfo; + *(CMapItemInfo *)&MapInfo = *(CMapItemInfo *)pData; MapInfo.m_Settings = SettingsIndex; - pData = (int *)&MapInfo; + pData = &MapInfo; Size = sizeof(MapInfo); } }