Scags / TF2Classic-Tools

Basic tools for TF2Classic dedicated server development
7 stars 11 forks source link

TF2_StunPlayer does not handle attackers with 0 indexes #2

Closed sappykun closed 4 years ago

sappykun commented 4 years ago

I'm trying to make a really half-assed port of pheadxdll's old Roll the Dice plugin.

There is a condition called "scary bullets" that is meant to scare players you damage. This uses Native_TF2_StunPlayer. However, when a player is attacked, the plugin throws this stacktrace:

L 07/11/2020 - 18:20:25: [SM] Exception reported: Client index 0 is invalid
L 07/11/2020 - 18:08:00: [SM] Blaming: custom/tf2c.smx
L 07/11/2020 - 18:08:00: [SM] Call stack trace:
L 07/11/2020 - 18:08:00: [SM]   [0] GetClientUserId
L 07/11/2020 - 18:08:00: [SM]   [1] Line 474, C:\Users\johnm\OneDrive\Documents\GitHub\TF2Classic-Tools\sourcemod\scripting\tf2c.sp::Native_TF2_StunPlayer
L 07/11/2020 - 18:08:00: [SM]   [3] TF2_StunPlayer
L 07/11/2020 - 18:08:00: [SM]   [4] Line 2499, C:\Users\Sap\Downloads\rtd_tf2c.sp::Event_PlayerHurt

Looking at the code in tf2c.sp, I noticed this block:

    if (attacker)
    {
        if (!(-1 <= attacker <= MaxClients))
            return ThrowNativeError(SP_ERROR_NATIVE, "Attacker index %d is invalid.", attacker);
        if (attacker > 0 && !IsClientInGame(attacker))
            return ThrowNativeError(SP_ERROR_NATIVE, "Attacker %d is not in-game", attacker);

        if (attacker == 0)
            attacker = -1;
//      if (GetClientTeam(attacker) == GetClientTeam(victim))
//          return;
    }

This code explicitly tries to set the attacker ID to -1 if their ID is 0, but since attacker is 0 and evaluates to false, this code is never reached. Therefore, the code tries to continue with a client index of 0 and ends up throwing the traceback.

I found that removing the if (attacker) clause entirely fixed the issue, but I'm not sure if that approach is safe or not.

Scags commented 4 years ago

Whoops, sorry about that. I'll fix that in a second. I should also warn you, the StunPlayer native isn't supported internally by TF2Classic, so YMMV.