Skip to content

Commit

Permalink
Stop EntRefToEntIndex returning garbage if a bad parameter is passed (#…
Browse files Browse the repository at this point in the history
…1323)

* Stop EntRefToEntIndex returning garbage if a bad parameter is passed

Seen multiple bad usage of this function that works only because whatever was passed in was returned as it wasnt an entity reference.
This code should have worked and would be expected to have returned something invalid but instead the the input was returned which allowed the code to work when really it is bad code.
See for one such case https://discordapp.com/channels/335290997317697536/335290997317697536/736518488314871868

* Update documentation of EntRefToEntIndex

Added the error text saying what shall be returned when a invalid parameter is passed.

* Validate entity index instead of just returning INVALID_EHANDLE_INDEX

Not sure if it needs this much validation but this just mirrors how IsValidEntity works, so the entity index returned should be valid else INVALID_EHANDLE_INDEX is returned.

* EntRefToEntIndex improve doc comments to better represent functionality

---------

Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
  • Loading branch information
c0rp3n and KyleSanderson committed Apr 25, 2024
1 parent 60b6a8d commit f9ad35b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 19 additions & 1 deletion core/HalfLife2.cpp
Expand Up @@ -1099,8 +1099,26 @@ int CHalfLife2::ReferenceToIndex(cell_t entRef)

return hndl.GetEntryIndex();
}
else
{
CEntInfo *pInfo = LookupEntity(entRef);
if (!pInfo)
{
return INVALID_EHANDLE_INDEX;
}
IServerUnknown *pUnk = static_cast<IServerUnknown *>(pInfo->m_pEntity);
if (!pUnk)
{
return INVALID_EHANDLE_INDEX;
}
CBaseEntity *pEntity = pUnk->GetBaseEntity();
if (!pEntity)
{
return INVALID_EHANDLE_INDEX;
}

return entRef;
return entRef;
}
}

cell_t CHalfLife2::EntityToBCompatRef(CBaseEntity *pEntity)
Expand Down
7 changes: 4 additions & 3 deletions plugins/include/halflife.inc
Expand Up @@ -667,10 +667,11 @@ stock void DisplayAskConnectBox(int client, float time, const char[] ip, const c
native int EntIndexToEntRef(int entity);

/**
* Retrieves the entity index from a reference.
* Retrieves the entity index from a reference or validates an entity index.
* The input ref is checked that it is still valid and refers to the same entity.
*
* @param ref Entity reference.
* @return Entity index or -1 on invalid reference.
* @param ref Entity reference or index.
* @return Entity index or returns INVALID_ENT_REFERENCE if ref is invalid.
*/
native int EntRefToEntIndex(int ref);

Expand Down

0 comments on commit f9ad35b

Please sign in to comment.