From 28937975600d5ab4041547be2395f6f9c90b9648 Mon Sep 17 00:00:00 2001 From: luttje <2738114+luttje@users.noreply.github.com> Date: Sat, 24 Aug 2024 17:24:36 +0200 Subject: [PATCH] Fix crash by incorrectly deleting recipfilter + let IsNnpc and IsPlayer return false on null safely --- .../modules/gmod_compatibility/sh_init.lua | 3 +- src/game/shared/lbaseentity_shared.cpp | 39 ++++++++++++------ src/game/shared/lbaseplayer_shared.cpp | 41 ++++--------------- 3 files changed, 38 insertions(+), 45 deletions(-) diff --git a/game/experiment/addons/gmod_compatibility/scripts/lua/includes/modules/gmod_compatibility/sh_init.lua b/game/experiment/addons/gmod_compatibility/scripts/lua/includes/modules/gmod_compatibility/sh_init.lua index b48db04580..b9e434768d 100644 --- a/game/experiment/addons/gmod_compatibility/scripts/lua/includes/modules/gmod_compatibility/sh_init.lua +++ b/game/experiment/addons/gmod_compatibility/scripts/lua/includes/modules/gmod_compatibility/sh_init.lua @@ -469,6 +469,7 @@ ENTITY_META.OBBMins = ENTITY_META.GetOBBMins ENTITY_META.LocalToWorld = ENTITY_META.EntityToWorldSpace ENTITY_META.SkinCount = ENTITY_META.GetSkinCount ENTITY_META.Alive = ENTITY_META.IsAlive +ENTITY_META.IsNPC = ENTITY_META.IsNpc ENTITY_META.Widget = false ENTITY_META.AddFlags = ENTITY_META.AddFlag ENTITY_META.RemoveFlags = ENTITY_META.RemoveFlag @@ -636,7 +637,7 @@ WEAPON_META.Ammo1 = WEAPON_META.GetPrimaryAmmoCount WEAPON_META.Ammo2 = WEAPON_META.GetSecondaryAmmoCount local PLAYER_META = FindMetaTable("Player") -PLAYER_META.GetShootPos = ENTITY_META.GetWeaponShootPosition +PLAYER_META.GetShootPos = PLAYER_META.GetWeaponShootPosition PLAYER_META.UserID = PLAYER_META.GetUserId PLAYER_META.AccountID = PLAYER_META.GetAccountId PLAYER_META.SteamID = PLAYER_META.GetSteamId diff --git a/src/game/shared/lbaseentity_shared.cpp b/src/game/shared/lbaseentity_shared.cpp index fe9a5aa99c..bb484a6e69 100644 --- a/src/game/shared/lbaseentity_shared.cpp +++ b/src/game/shared/lbaseentity_shared.cpp @@ -387,13 +387,13 @@ LUA_BINDING_BEGIN( Entity, EmitSound, "class", "Emit sound." ) int nSoundFlags = LUA_BINDING_ARGUMENT_WITH_DEFAULT( luaL_optnumber, 7, 0, "soundFlags" ); int nDSP = LUA_BINDING_ARGUMENT_WITH_DEFAULT( luaL_optnumber, 8, 0, "dsp" ); - CRecipientFilter *filter = nullptr; + CRecipientFilter filter; if ( lua_isrecipientfilter( L, 9 ) ) - filter = &LUA_BINDING_ARGUMENT_NILLABLE( luaL_checkrecipientfilter, 9, "filter" ); + filter = LUA_BINDING_ARGUMENT_NILLABLE( luaL_checkrecipientfilter, 9, "filter" ); else { - filter = new CPASAttenuationFilter( pEntity, soundLevel ); + filter = CPASAttenuationFilter( pEntity, soundLevel ); } float duration = 0; @@ -411,14 +411,12 @@ LUA_BINDING_BEGIN( Entity, EmitSound, "class", "Emit sound." ) params.m_flSoundTime = 0; params.m_nSpeakerEntity = iEntIndex; params.m_pflSoundDuration = &duration; - params.m_bWarnOnDirectWaveReference = true; + params.m_bWarnOnDirectWaveReference = false; - pEntity->EmitSound( *filter, iEntIndex, params ); + pEntity->EmitSound( filter, iEntIndex, params ); lua_pushnumber( L, duration ); - delete[] filter; - return 1; } LUA_BINDING_END( "number", "The sound duration." ) @@ -1029,7 +1027,7 @@ LUA_BINDING_BEGIN( Entity, GetRefTable, "class", "Get reference table." ) // have a nil reference table returned. lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( lua_toentity, 1, "entity" ); - if (pEntity == NULL) + if ( pEntity == NULL ) { lua_pushnil( L ); } @@ -1396,9 +1394,15 @@ LUA_BINDING_BEGIN( Entity, IsMarkedForDeletion, "class", "Is marked for deletion } LUA_BINDING_END( "boolean", "True if marked for deletion, false otherwise." ) -LUA_BINDING_BEGIN( Entity, IsNPC, "class", "Is NPC." ) +LUA_BINDING_BEGIN( Entity, IsNpc, "class", "Is NPC." ) { - lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( luaL_checkentity, 1, "entity" ); + lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( lua_toentity, 1, "entity" ); + + if ( !pEntity ) + { + lua_pushboolean( L, false ); + return 1; + } lua_pushboolean( L, pEntity->IsNPC() ); return 1; @@ -1407,7 +1411,13 @@ LUA_BINDING_END( "boolean", "True if NPC, false otherwise." ) LUA_BINDING_BEGIN( Entity, IsPlayer, "class", "Is player." ) { - lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( luaL_checkentity, 1, "entity" ); + lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( lua_toentity, 1, "entity" ); + + if ( !pEntity ) + { + lua_pushboolean( L, false ); + return 1; + } lua_pushboolean( L, pEntity->IsPlayer() ); return 1; @@ -2815,7 +2825,7 @@ LUA_BINDING_BEGIN( Entity, SetNetworkDataValue, "class", "Sets a data table vari { int newValueInt = LUA_BINDING_ARGUMENT( luaL_checknumber, 4, "value" ); lua_pushinteger( L, entity->m_LuaVariables_int[slot] ); - lua_pushnumber( L, newValueInt ); + lua_pushinteger( L, newValueInt ); #ifdef CLIENT_DLL entity->m_LuaVariables_int[slot] = newValueInt; #else @@ -2828,6 +2838,11 @@ LUA_BINDING_BEGIN( Entity, SetNetworkDataValue, "class", "Sets a data table vari float newValueFloat = LUA_BINDING_ARGUMENT( luaL_checknumber, 4, "value" ); lua_pushnumber( L, entity->m_LuaVariables_float[slot] ); lua_pushnumber( L, newValueFloat ); + if ( !IsFinite( newValueFloat ) ) + { + newValueFloat = FLT_MAX; + Assert( IsFinite( newValueFloat ) ); + } #ifdef CLIENT_DLL entity->m_LuaVariables_float[slot] = newValueFloat; #else diff --git a/src/game/shared/lbaseplayer_shared.cpp b/src/game/shared/lbaseplayer_shared.cpp index 509cb6b9ce..7c3a14dd69 100644 --- a/src/game/shared/lbaseplayer_shared.cpp +++ b/src/game/shared/lbaseplayer_shared.cpp @@ -1549,22 +1549,12 @@ LUA_BINDING_END( "integer", "Team of the local player." ) LUA_BINDING_BEGIN( Players, GetAllBots, "library", "Get all bots." ) { - CUtlVector< CBasePlayer * > curPlayers; + lua_newtable( L ); for ( int i = 1; i <= gpGlobals->maxClients; i++ ) { CBasePlayer *pPlayer = UTIL_PlayerByIndex( i ); - if ( pPlayer && ( pPlayer->GetFlags() & FL_FAKECLIENT ) ) - { - curPlayers.AddToTail( pPlayer ); - } - } - - lua_newtable( L ); - - for ( int i = 0; i < curPlayers.Count(); i++ ) - { - CBaseEntity::PushLuaInstanceSafe( L, curPlayers[i] ); + CBaseEntity::PushLuaInstanceSafe( L, pPlayer ); lua_rawseti( L, -2, i + 1 ); } @@ -1574,50 +1564,37 @@ LUA_BINDING_END( "table", "All bots." ) LUA_BINDING_BEGIN( Players, GetAllHumans, "library", "Get all humans." ) { - CUtlVector< CBasePlayer * > curPlayers; + lua_newtable( L ); for ( int i = 1; i <= gpGlobals->maxClients; i++ ) { CBasePlayer *pPlayer = UTIL_PlayerByIndex( i ); + if ( pPlayer && !( pPlayer->GetFlags() & FL_FAKECLIENT ) ) { - curPlayers.AddToTail( pPlayer ); + CBaseEntity::PushLuaInstanceSafe( L, pPlayer ); + lua_rawseti( L, -2, i + 1 ); } } - lua_newtable( L ); - - for ( int i = 0; i < curPlayers.Count(); i++ ) - { - CBaseEntity::PushLuaInstanceSafe( L, curPlayers[i] ); - lua_rawseti( L, -2, i + 1 ); - } - return 1; } LUA_BINDING_END( "table", "All humans." ) LUA_BINDING_BEGIN( Players, GetAll, "library", "Get all players." ) { - CUtlVector< CBasePlayer * > curPlayers; + lua_newtable( L ); for ( int i = 1; i <= gpGlobals->maxClients; i++ ) { CBasePlayer *pPlayer = UTIL_PlayerByIndex( i ); if ( pPlayer ) { - curPlayers.AddToTail( pPlayer ); + CBaseEntity::PushLuaInstanceSafe( L, pPlayer ); + lua_rawseti( L, -2, i + 1 ); } } - lua_newtable( L ); - - for ( int i = 0; i < curPlayers.Count(); i++ ) - { - CBaseEntity::PushLuaInstanceSafe( L, curPlayers[i] ); - lua_rawseti( L, -2, i + 1 ); - } - return 1; } LUA_BINDING_END( "table", "All players." )