Core/OutdoorPvP: Fixed crash happening by accessing freed memory

This commit is contained in:
Shauren authored and Ovahlord committed 2023-01-09 18:19:08 +01:00
1 parent 684592851c
commit 3e8f413b0c
4 files changed
+101 -13

No files matched your search

+71
View File
@@ -272,6 +272,77 @@ namespace Trinity
++itr;
}
}
namespace Impl
{
template <typename Container, typename Predicate>
void EraseIfMoveAssignable(Container& c, Predicate p)
{
auto wpos = c.begin();
for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos)
{
if (!p(*rpos))
{
if (rpos != wpos)
std::swap(*rpos, *wpos);
++wpos;
}
}
c.erase(wpos, c.end());
}
template <typename Container, typename Predicate>
void EraseIfNotMoveAssignable(Container& c, Predicate p)
{
for (auto it = c.begin(); it != c.end();)
{
if (p(*it))
it = c.erase(it);
else
++it;
}
}
}
template <typename Container, typename Predicate>
void EraseIf(Container& c, Predicate p)
{
if constexpr (std::is_move_assignable_v<decltype(*c.begin())>)
Impl::EraseIfMoveAssignable(c, std::ref(p));
else
Impl::EraseIfNotMoveAssignable(c, std::ref(p));
}
/**
* Returns a mutable reference to element at index i
* Will resize vector if neccessary to ensure element at i can be safely written
*
* This exists as separate overload instead of one function with default argument to allow using
* with vectors of non-default-constructible classes
*/
template<typename T>
inline decltype(auto) EnsureWritableVectorIndex(std::vector<T>& vec, typename std::vector<T>::size_type i)
{
if (i >= vec.size())
vec.resize(i + 1);
return vec[i];
}
/**
* Returns a mutable reference to element at index i
* Will resize vector if neccessary to ensure element at i can be safely written
*
* This overload allows specifying what value to pad vector with during .resize
*/
template<typename T>
inline decltype(auto) EnsureWritableVectorIndex(std::vector<T>& vec, typename std::vector<T>::size_type i, T const& resizeDefault)
{
if (i >= vec.size())
vec.resize(i + 1, resizeDefault);
return vec[i];
}
}
//! namespace Containers
}
+20
View File
@@ -434,6 +434,26 @@ void OutdoorPvP::BroadcastPacket(WorldPacket &data) const
player->SendDirectMessage(&data);
}
void OutdoorPvP::AddCapturePoint(OPvPCapturePoint* cp)
{
OPvPCapturePointMap::iterator i = m_capturePoints.find(cp->m_capturePointSpawnId);
if (i != m_capturePoints.end())
{
TC_LOG_ERROR("outdoorpvp", "OutdoorPvP::AddCapturePoint: CapturePoint {} already exists!", cp->m_capturePointSpawnId);
if (i->second.get() == cp)
return;
}
m_capturePoints[cp->m_capturePointSpawnId].reset(cp);
}
OPvPCapturePoint* OutdoorPvP::GetCapturePoint(ObjectGuid::LowType guid) const
{
OutdoorPvP::OPvPCapturePointMap::const_iterator itr = m_capturePoints.find(guid);
if (itr != m_capturePoints.end())
return itr->second.get();
return nullptr;
}
void OutdoorPvP::RegisterZone(uint32 zoneId)
{
sOutdoorPvPMgr->AddZone(zoneId, this);
+5 -13
View File
@@ -23,6 +23,7 @@
#include "SharedDefines.h"
#include "ZoneScript.h"
#include <map>
#include <memory>
namespace WorldPackets
{
@@ -169,7 +170,8 @@ class TC_GAME_API OutdoorPvP : public ZoneScript
// dtor
virtual ~OutdoorPvP();
typedef std::map<ObjectGuid::LowType/*spawnId*/, OPvPCapturePoint*> OPvPCapturePointMap;
typedef std::map<ObjectGuid::LowType/*spawnId*/, std::unique_ptr<OPvPCapturePoint>> OPvPCapturePointMap;
typedef std::pair<ObjectGuid::LowType, GameObject*> GoScriptPair;
typedef std::pair<ObjectGuid::LowType, Creature*> CreatureScriptPair;
@@ -251,19 +253,9 @@ class TC_GAME_API OutdoorPvP : public ZoneScript
virtual void HandlePlayerResurrects(Player* player, uint32 zone);
void AddCapturePoint(OPvPCapturePoint* cp)
{
m_capturePoints[cp->m_capturePointSpawnId] = cp;
}
void AddCapturePoint(OPvPCapturePoint* cp);
OPvPCapturePoint* GetCapturePoint(ObjectGuid::LowType spawnId) const
{
OutdoorPvP::OPvPCapturePointMap::const_iterator itr = m_capturePoints.find(spawnId);
if (itr != m_capturePoints.end())
return itr->second;
return nullptr;
}
OPvPCapturePoint* GetCapturePoint(ObjectGuid::LowType spawnId) const;
void RegisterZone(uint32 zoneid);
@@ -16,6 +16,7 @@
*/
#include "OutdoorPvPMgr.h"
#include "Containers.h"
#include "DatabaseEnv.h"
#include "DisableMgr.h"
#include "Log.h"
@@ -118,6 +119,10 @@ void OutdoorPvPMgr::CreateOutdoorPvPForMap(Map* map)
void OutdoorPvPMgr::DestroyOutdoorPvPForMap(Map* map)
{
Trinity::Containers::EraseIf(m_OutdoorPvPMap, [map](OutdoorPvPMap::value_type const& pair)
{
return pair.first.first == map;
});
m_OutdoorPvPByMap.erase(map);
}