Skip to content

Commit

Permalink
Prevent using dangling pointer to HonorStanding.
Browse files Browse the repository at this point in the history
Closes #500
  • Loading branch information
mostlikely4r authored and killerwife committed May 25, 2023
1 parent 6c9e102 commit 6516f8e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/game/Entities/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6448,7 +6448,7 @@ void Player::UpdateHonor()
SetHonorLastWeekStandingPos(sObjectMgr.GetHonorStandingPositionByGUID(GetGUIDLow(), GetTeam()));

// RANK POINTS
HonorStanding* standing = sObjectMgr.GetHonorStandingByGUID(GetGUIDLow(), GetTeam());
std::optional<HonorStanding> standing = sObjectMgr.GetHonorStandingByGUID(GetGUIDLow(), GetTeam());
float rankP = GetStoredHonor();
if (standing)
rankP += standing->rpEarning;
Expand Down
12 changes: 6 additions & 6 deletions src/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3681,31 +3681,31 @@ HonorStandingList ObjectMgr::GetStandingListBySide(uint32 side)
}
}

HonorStanding* ObjectMgr::GetHonorStandingByGUID(uint32 guid, uint32 side)
std::optional<HonorStanding> ObjectMgr::GetHonorStandingByGUID(uint32 guid, uint32 side)
{
HonorStandingList standingList = sObjectMgr.GetStandingListBySide(side);

for (HonorStandingList::iterator itr = standingList.begin(); itr != standingList.end() ; ++itr)
if (itr->guid == guid)
return itr->GetInfo();
return *itr;

return 0;
return {};
}


HonorStanding* ObjectMgr::GetHonorStandingByPosition(uint32 position, uint32 side)
std::optional<HonorStanding> ObjectMgr::GetHonorStandingByPosition(uint32 position, uint32 side)
{
HonorStandingList standingList = sObjectMgr.GetStandingListBySide(side);
uint32 pos = 1;

for (HonorStandingList::iterator itr = standingList.begin(); itr != standingList.end() ; ++itr)
{
if (pos == position)
return itr->GetInfo();
return *itr;
pos++;
}

return 0;
return {};
}

uint32 ObjectMgr::GetHonorStandingPositionByGUID(uint32 guid, uint32 side)
Expand Down
5 changes: 3 additions & 2 deletions src/game/Globals/ObjectMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <climits>
#include <memory>
#include <tuple>
#include <optional>

class Group;
class Item;
Expand Down Expand Up @@ -767,8 +768,8 @@ class ObjectMgr
return itr != mFishingBaseForArea.end() ? itr->second : 0;
}

static HonorStanding* GetHonorStandingByGUID(uint32 guid, uint32 side);
static HonorStanding* GetHonorStandingByPosition(uint32 position, uint32 side);
static std::optional<HonorStanding> GetHonorStandingByGUID(uint32 guid, uint32 side);
static std::optional<HonorStanding> GetHonorStandingByPosition(uint32 position, uint32 side);
HonorStandingList GetStandingListBySide(uint32 side);
uint32 GetHonorStandingPositionByGUID(uint32 guid, uint32 side);
void UpdateHonorStandingByGuid(uint32 guid, HonorStanding standing, uint32 side) ;
Expand Down
4 changes: 2 additions & 2 deletions src/game/Tools/Formulas.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace MaNGOS

// the X values for each breakpoint are found from the CP scores
// of the players around that point in the WS scores
HonorStanding* tempSt;
HonorStanding tempSt;
float honor;

// initialize CP array
Expand All @@ -151,7 +151,7 @@ namespace MaNGOS
for (uint8 i = 1; i <= 13; i++)
{
honor = 0.0f;
tempSt = sObjectMgr.GetHonorStandingByPosition(sc.BRK[i], team);
std::optional<HonorStanding> tempSt = sObjectMgr.GetHonorStandingByPosition(sc.BRK[i], team);
if (tempSt)
{
honor += tempSt->honorPoints;
Expand Down

0 comments on commit 6516f8e

Please sign in to comment.