Skip to content

Commit

Permalink
Add safe check for weapon natives (make sure it's a CBasePlayerWeapon…
Browse files Browse the repository at this point in the history
… instance)
  • Loading branch information
s1lentq committed Feb 1, 2024
1 parent f2ef526 commit fbe2788
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions reapi/src/natives/natives_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define CHECK_ISENTITY(x) if (unlikely(params[x] < 0 || params[x] > gpGlobals->maxEntities)) { AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid entity index %i [%s]", __FUNCTION__, params[x], #x); return FALSE; }
#define CHECK_GAMERULES() if (unlikely(!g_pGameRules)) { AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: gamerules not initialized", __FUNCTION__); return FALSE; }
#define CHECK_CONNECTED(x, y) if (unlikely(x == nullptr || x->has_disconnected)) { AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: player %i is not connected", __FUNCTION__, params[y]); return FALSE; }
#define CHECK_INSTANCE_OF(x, y) if (unlikely(dynamic_cast<x *>((x::BaseClass *)y) == nullptr)) { AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid entity %d ('%s'), is not an instance of the base class '%s'", __FUNCTION__, indexOfEdict(y->pev), STRING(y->pev->classname), #x); return FALSE; }

class CAmxArg
{
Expand Down
50 changes: 40 additions & 10 deletions reapi/src/natives/natives_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,8 +1675,15 @@ cell AMX_NATIVE_CALL rg_instant_reload_weapons(AMX *amx, cell *params)
if (params[arg_weapon] != 0)
{
pWeapon = getPrivate<CBasePlayerWeapon>(params[arg_weapon]);
if (!pWeapon || !pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: Invalid entity weapon", __FUNCTION__);
if (unlikely(pWeapon == nullptr)) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid or uninitialized entity", __FUNCTION__);
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
return FALSE;
}
}
Expand Down Expand Up @@ -1867,9 +1874,16 @@ cell AMX_NATIVE_CALL rg_switch_weapon(AMX *amx, cell *params)
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);

auto pWeapon = getPrivate<CBasePlayerWeapon>(params[arg_weapon]);
if (pWeapon == nullptr || !pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: Invalid entity weapon", __FUNCTION__);
CBasePlayerWeapon *pWeapon = getPrivate<CBasePlayerWeapon>(params[arg_weapon]);
if (unlikely(pWeapon == nullptr)) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid or uninitialized entity", __FUNCTION__);
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
return FALSE;
}

Expand Down Expand Up @@ -2142,6 +2156,8 @@ cell AMX_NATIVE_CALL rg_set_iteminfo(AMX *amx, cell *params)
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
return FALSE;
Expand Down Expand Up @@ -2205,6 +2221,8 @@ cell AMX_NATIVE_CALL rg_get_iteminfo(AMX *amx, cell *params)
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
return FALSE;
Expand Down Expand Up @@ -2834,6 +2852,8 @@ cell AMX_NATIVE_CALL rg_weapon_deploy(AMX* amx, cell* params)
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon())
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
Expand Down Expand Up @@ -2898,6 +2918,8 @@ cell AMX_NATIVE_CALL rg_weapon_reload(AMX* amx, cell* params)
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon())
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
Expand Down Expand Up @@ -2968,6 +2990,8 @@ cell AMX_NATIVE_CALL rg_weapon_shotgun_reload(AMX* amx, cell* params)
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon())
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
Expand Down Expand Up @@ -3032,6 +3056,8 @@ cell AMX_NATIVE_CALL rg_weapon_send_animation(AMX* amx, cell* params)
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon())
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
Expand Down Expand Up @@ -3097,6 +3123,8 @@ cell AMX_NATIVE_CALL rg_weapon_kickback(AMX* amx, cell* params)
return FALSE;
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon())
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
Expand Down Expand Up @@ -3151,11 +3179,6 @@ cell AMX_NATIVE_CALL rg_switch_best_weapon(AMX* amx, cell* params)
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid or uninitialized entity", __FUNCTION__);
return FALSE;
}

if (!pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
return FALSE;
}
}
else
{
Expand All @@ -3167,6 +3190,13 @@ cell AMX_NATIVE_CALL rg_switch_best_weapon(AMX* amx, cell* params)
}
}

CHECK_INSTANCE_OF(CBasePlayerWeapon, pWeapon);

if (!pWeapon->IsWeapon()) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: #%d entity is not a weapon.", __FUNCTION__, indexOfEdict(pWeapon->pev));
return FALSE;
}

return CSGameRules()->GetNextBestWeapon(pPlayer, pWeapon);
}

Expand Down

0 comments on commit fbe2788

Please sign in to comment.