Skip to content

Commit

Permalink
Client: HUD: DeathNotice: Check for invalid players (#187)
Browse files Browse the repository at this point in the history
Resolves #187
  • Loading branch information
tmp64 committed Dec 3, 2023
1 parent bef7c1b commit c046f17
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/game/client/hud/death_notice_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ void CHudDeathNoticePanel::Think()
void CHudDeathNoticePanel::AddItem(int killerId, int victimId, const char *killedwith)
{
Entry e;
CPlayerInfo *killer = (killerId >= 1 && killerId <= MAX_PLAYERS) ? GetPlayerInfo(killerId) : nullptr;
CPlayerInfo *victim = (victimId >= 1 && victimId <= MAX_PLAYERS) ? GetPlayerInfo(victimId) : nullptr;
CPlayerInfo *killer = GetPlayerInfoSafe(killerId);
CPlayerInfo *victim = GetPlayerInfoSafe(victimId);
int thisPlayerId = GetThisPlayerInfo()->GetIndex();

// Check for suicide
Expand Down
12 changes: 12 additions & 0 deletions src/game/client/player_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ CPlayerInfo *GetThisPlayerInfo()
return s_ThisPlayerInfo;
}

CPlayerInfo *GetPlayerInfoSafe(int idx)
{
if (!(idx >= 1 && idx <= MAX_PLAYERS))
return nullptr;

CPlayerInfo *pInfo = GetPlayerInfo(idx);
if (!pInfo->IsConnected())
return nullptr;

return pInfo;
}

int CPlayerInfo::GetIndex()
{
return m_iIndex;
Expand Down
6 changes: 6 additions & 0 deletions src/game/client/player_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class CPlayerInfo;
CPlayerInfo *GetPlayerInfo(int idx);
CPlayerInfo *GetThisPlayerInfo();

//! Gets the player info for player index. Checks that the index
//! is valid and the player is connected.
//! @param idx Player index in the range [1; MAX_PLAYERS].
//! @returns Player info or nullptr.
CPlayerInfo *GetPlayerInfoSafe(int idx);

class CPlayerInfo
{
public:
Expand Down

0 comments on commit c046f17

Please sign in to comment.