Skip to content

Fix memory overwriting by EnumToString & StringToEnum #4089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Client/core/CConnectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*****************************************************************************/

#include "StdInc.h"
#include "net/packetenums.h"
#include "net/Packets.h"
using namespace std;

static CConnectManager* g_pConnectManager = NULL;
Expand Down
38 changes: 19 additions & 19 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
//

IMPLEMENT_ENUM_BEGIN(eLuaType)
ADD_ENUM(LUA_TNONE, "none")
ADD_ENUM(LUA_TNIL, "nil")
ADD_ENUM(LUA_TBOOLEAN, "boolean")
ADD_ENUM(LUA_TLIGHTUSERDATA, "lightuserdata")
ADD_ENUM(LUA_TNUMBER, "number")
ADD_ENUM(LUA_TSTRING, "string")
ADD_ENUM(LUA_TTABLE, "table")
ADD_ENUM(LUA_TFUNCTION, "function")
ADD_ENUM(LUA_TUSERDATA, "userdata")
ADD_ENUM(LUA_TTHREAD, "thread")
ADD_ENUM(static_cast<eLuaType>(LUA_TNONE), "none")
ADD_ENUM(static_cast<eLuaType>(LUA_TNIL), "nil")
ADD_ENUM(static_cast<eLuaType>(LUA_TBOOLEAN), "boolean")
ADD_ENUM(static_cast<eLuaType>(LUA_TLIGHTUSERDATA), "lightuserdata")
ADD_ENUM(static_cast<eLuaType>(LUA_TNUMBER), "number")
ADD_ENUM(static_cast<eLuaType>(LUA_TSTRING), "string")
ADD_ENUM(static_cast<eLuaType>(LUA_TTABLE), "table")
ADD_ENUM(static_cast<eLuaType>(LUA_TFUNCTION), "function")
ADD_ENUM(static_cast<eLuaType>(LUA_TUSERDATA), "userdata")
ADD_ENUM(static_cast<eLuaType>(LUA_TTHREAD), "thread")
IMPLEMENT_ENUM_END("lua-type")

IMPLEMENT_ENUM_BEGIN(CGUIVerticalAlign)
Expand Down Expand Up @@ -715,15 +715,15 @@ IMPLEMENT_ENUM_CLASS_END("client-model-type")

// Sound effects
IMPLEMENT_ENUM_BEGIN(eSoundEffectType)
ADD_ENUM(BASS_FX_DX8_CHORUS, "chorus")
ADD_ENUM(BASS_FX_DX8_COMPRESSOR, "compressor")
ADD_ENUM(BASS_FX_DX8_DISTORTION, "distortion")
ADD_ENUM(BASS_FX_DX8_ECHO, "echo")
ADD_ENUM(BASS_FX_DX8_FLANGER, "flanger")
ADD_ENUM(BASS_FX_DX8_GARGLE, "gargle")
ADD_ENUM(BASS_FX_DX8_I3DL2REVERB, "i3dl2reverb")
ADD_ENUM(BASS_FX_DX8_PARAMEQ, "parameq")
ADD_ENUM(BASS_FX_DX8_REVERB, "reverb")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_CHORUS), "chorus")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_COMPRESSOR), "compressor")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_DISTORTION), "distortion")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_ECHO), "echo")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_FLANGER), "flanger")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_GARGLE), "gargle")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_I3DL2REVERB), "i3dl2reverb")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_PARAMEQ), "parameq")
ADD_ENUM(static_cast<eSoundEffectType>(BASS_FX_DX8_REVERB), "reverb")
IMPLEMENT_ENUM_END("soundeffect-type")

IMPLEMENT_ENUM_CLASS_BEGIN(eSoundEffectParams::Chorus)
Expand Down
20 changes: 10 additions & 10 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
//

IMPLEMENT_ENUM_BEGIN(eLuaType)
ADD_ENUM(LUA_TNONE, "none")
ADD_ENUM(LUA_TNIL, "nil")
ADD_ENUM(LUA_TBOOLEAN, "boolean")
ADD_ENUM(LUA_TLIGHTUSERDATA, "lightuserdata")
ADD_ENUM(LUA_TNUMBER, "number")
ADD_ENUM(LUA_TSTRING, "string")
ADD_ENUM(LUA_TTABLE, "table")
ADD_ENUM(LUA_TFUNCTION, "function")
ADD_ENUM(LUA_TUSERDATA, "userdata")
ADD_ENUM(LUA_TTHREAD, "thread")
ADD_ENUM(static_cast<eLuaType>(LUA_TNONE), "none")
ADD_ENUM(static_cast<eLuaType>(LUA_TNIL), "nil")
ADD_ENUM(static_cast<eLuaType>(LUA_TBOOLEAN), "boolean")
ADD_ENUM(static_cast<eLuaType>(LUA_TLIGHTUSERDATA), "lightuserdata")
ADD_ENUM(static_cast<eLuaType>(LUA_TNUMBER), "number")
ADD_ENUM(static_cast<eLuaType>(LUA_TSTRING), "string")
ADD_ENUM(static_cast<eLuaType>(LUA_TTABLE), "table")
ADD_ENUM(static_cast<eLuaType>(LUA_TFUNCTION), "function")
ADD_ENUM(static_cast<eLuaType>(LUA_TUSERDATA), "userdata")
ADD_ENUM(static_cast<eLuaType>(LUA_TTHREAD), "thread")
IMPLEMENT_ENUM_END("lua-type")

IMPLEMENT_ENUM_BEGIN(TrafficLight::EColor)
Expand Down
53 changes: 22 additions & 31 deletions Shared/sdk/SharedUtil.Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1265,10 +1265,6 @@ namespace SharedUtil
//
// enum reflection shenanigans
//
enum eDummy
{
};

template <class T>
struct CEnumInfo
{
Expand All @@ -1278,31 +1274,31 @@ namespace SharedUtil
const char* szName;
};

CEnumInfo(const SString& strTypeName, const SEnumItem* pItemList, uint uiAmount, eDummy defaultValue, const SString& strDefaultName)
CEnumInfo(const SString& strTypeName, const SEnumItem* pItemList, uint uiAmount, T defaultValue, const SString& strDefaultName)
{
m_strTypeName = strTypeName;
m_strDefaultName = strDefaultName;
m_DefaultValue = defaultValue;
for (uint i = 0; i < uiAmount; i++)
{
const SEnumItem& item = pItemList[i];
m_ValueMap[item.szName] = (eDummy)item.iValue;
m_NameMap[(eDummy)item.iValue] = item.szName;
m_ValueMap[item.szName] = item.iValue;
m_NameMap[item.iValue] = item.szName;
}
}

bool ValueValid(eDummy value) const { return MapContains(m_NameMap, value); }
bool ValueValid(T value) const { return MapContains(m_NameMap, value); }

const SString& FindName(eDummy value) const
const SString& FindName(T value) const
{
if (const SString* pName = MapFind(m_NameMap, value))
return *pName;
return m_strDefaultName;
}

bool FindValue(const SString& strName, eDummy& outResult) const
bool FindValue(const SString& strName, T& outResult) const
{
const eDummy* pValue;
const T* pValue;
if ((pValue = MapFind(m_ValueMap, strName)) || (pValue = MapFind(m_ValueMap, strName.ToLower())))
{
outResult = *pValue;
Expand All @@ -1316,45 +1312,40 @@ namespace SharedUtil

SString m_strTypeName;
SString m_strDefaultName;
eDummy m_DefaultValue;
std::map<SString, eDummy> m_ValueMap;
std::map<eDummy, SString> m_NameMap;
T m_DefaultValue;
std::map<SString, T> m_ValueMap;
std::map<T, SString> m_NameMap;
};

#define DECLARE_ENUM2(T, U) \
CEnumInfo<U>* GetEnumInfo ( const T* ); \
inline const SString& EnumToString ( const T& value ) { return GetEnumInfo ( (T*)0 )->FindName ( (eDummy)value ); }\
inline bool StringToEnum ( const SString& strName, T& outResult ) { return GetEnumInfo ( (T*)0 )->FindValue ( strName, (eDummy&)outResult ); }\
#define DECLARE_ENUM(T) \
CEnumInfo<T>* GetEnumInfo ( const T* ); \
inline const SString& EnumToString ( const T& value ) { return GetEnumInfo ( (T*)0 )->FindName ( value ); }\
inline bool StringToEnum ( const SString& strName, T& outResult ) { return GetEnumInfo ( (T*)0 )->FindValue ( strName, outResult ); }\
inline const SString& GetEnumTypeName ( const T& ) { return GetEnumInfo ( (T*)0 )->GetTypeName (); }\
inline bool EnumValueValid ( const T& value ) { return GetEnumInfo ( (T*)0 )->ValueValid ( (eDummy)value ); }\
inline bool EnumValueValid ( const T& value ) { return GetEnumInfo ( (T*)0 )->ValueValid ( value ); }\

#define IMPLEMENT_ENUM_BEGIN2(T, U) \
CEnumInfo<U>* GetEnumInfo( const T* ) \
#define IMPLEMENT_ENUM_BEGIN(T) \
CEnumInfo<T>* GetEnumInfo( const T* ) \
{ \
using CEnumInfo = CEnumInfo<U>; \
using CEnumInfo = CEnumInfo<T>; \
static const CEnumInfo::SEnumItem items[] = {

#define IMPLEMENT_ENUM_END(name) \
IMPLEMENT_ENUM_END_DEFAULTS(name,0,"")
IMPLEMENT_ENUM_END_DEFAULTS(name, static_cast<std::remove_reference_t<decltype(std::declval<CEnumInfo::SEnumItem>().iValue)>>(0), "")

#define IMPLEMENT_ENUM_END_DEFAULTS(name,defvalue,defname) \
}; \
static CEnumInfo info( name, items, NUMELMS(items),(eDummy)(defvalue),defname ); \
static CEnumInfo info(name, items, NUMELMS(items), defvalue, defname); \
return &info; \
}

#define ADD_ENUM(value,name) {value, name},
#define ADD_ENUM1(value) {value, #value},

// enum
#define DECLARE_ENUM(T) DECLARE_ENUM2(T, int)
#define IMPLEMENT_ENUM_BEGIN(T) IMPLEMENT_ENUM_BEGIN2(T, int)

// enum class
#define DECLARE_ENUM_CLASS(T) DECLARE_ENUM2(T, T)
#define IMPLEMENT_ENUM_CLASS_BEGIN(T) IMPLEMENT_ENUM_BEGIN2(T, T)
#define DECLARE_ENUM_CLASS(T) DECLARE_ENUM(T)
#define IMPLEMENT_ENUM_CLASS_BEGIN(T) IMPLEMENT_ENUM_BEGIN(T)
#define IMPLEMENT_ENUM_CLASS_END(name) IMPLEMENT_ENUM_END(name)
#define IMPLEMENT_ENUM_CLASS_END_DEFAULTS(name,defvalue,defname) IMPLEMENT_ENUM_END_DEFAULTS(name,defvalue,defname)

//
// Fast wildcard matching
Expand Down
28 changes: 27 additions & 1 deletion Shared/sdk/net/Packets.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,34 @@

enum ePacketID
{
// Internal MTA packets
PACKET_ID_SERVER_JOIN = 0,
PACKET_ID_SERVER_JOIN_DATA,
PACKET_ID_SERVER_JOIN_COMPLETE,

PACKET_ID_PLAYER_JOIN,
PACKET_ID_PLAYER_JOINDATA,
PACKET_ID_PLAYER_QUIT,
PACKET_ID_PLAYER_TIMEOUT,

PACKET_ID_MOD_NAME,
PACKET_ID_PACKET_PROGRESS,
PACKET_ID_MTA_RESERVED_03,
PACKET_ID_MTA_RESERVED_04,
PACKET_ID_MTA_RESERVED_05,
PACKET_ID_MTA_RESERVED_06,
PACKET_ID_MTA_RESERVED_07,
PACKET_ID_MTA_RESERVED_08,
PACKET_ID_MTA_RESERVED_09,
PACKET_ID_MTA_RESERVED_10,
PACKET_ID_MTA_RESERVED_11,
PACKET_ID_MTA_RESERVED_12,
PACKET_ID_MTA_RESERVED_13,
PACKET_ID_MTA_RESERVED_14,
PACKET_ID_MTA_RESERVED_15,

// Connection packets
PACKET_ID_SERVER_JOINEDGAME = PACKET_ID_END_OF_INTERNAL_PACKETS,
PACKET_ID_SERVER_JOINEDGAME,
PACKET_ID_SERVER_DISCONNECTED,

// All our outgoing only packets use this
Expand Down
33 changes: 1 addition & 32 deletions Shared/sdk/net/packetenums.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,6 @@
*****************************************************************************/

#pragma once

enum
{
PACKET_ID_SERVER_JOIN = 0,
PACKET_ID_SERVER_JOIN_DATA,
PACKET_ID_SERVER_JOIN_COMPLETE,

PACKET_ID_PLAYER_JOIN,
PACKET_ID_PLAYER_JOINDATA,
PACKET_ID_PLAYER_QUIT,
PACKET_ID_PLAYER_TIMEOUT,

PACKET_ID_MOD_NAME,
PACKET_ID_PACKET_PROGRESS,
PACKET_ID_MTA_RESERVED_03,
PACKET_ID_MTA_RESERVED_04,
PACKET_ID_MTA_RESERVED_05,
PACKET_ID_MTA_RESERVED_06,
PACKET_ID_MTA_RESERVED_07,
PACKET_ID_MTA_RESERVED_08,
PACKET_ID_MTA_RESERVED_09,
PACKET_ID_MTA_RESERVED_10,
PACKET_ID_MTA_RESERVED_11,
PACKET_ID_MTA_RESERVED_12,
PACKET_ID_MTA_RESERVED_13,
PACKET_ID_MTA_RESERVED_14,
PACKET_ID_MTA_RESERVED_15,

PACKET_ID_END_OF_INTERNAL_PACKETS
};

#define RAKNET_PACKET_COUNT RID_USER_PACKET_ENUM

/// You should not edit the file MessageIdentifiers.h as it is a part of RakNet static library
Expand All @@ -55,7 +24,7 @@ enum
/// \endcode
///
/// \note All these enumerations should be casted to (unsigned char) before writing them to RakNet::BitStream
enum
enum eRakNetPackets
{
//
// RESERVED TYPES - DO NOT CHANGE THESE
Expand Down
Loading