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.
This commit is contained in:
Robert Müller 2022-11-12 20:04:21 +01:00
parent 79df5893ff
commit ea06a13429

View file

@ -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);
}
}