Skip to content

Commit

Permalink
Protect Player::hud from concurrent modifications
Browse files Browse the repository at this point in the history
Sometimes HUD can be modified by ServerThread and EmergeThread results in a crash on client side because the HUD is not correct
  • Loading branch information
nerzhul committed Mar 22, 2015
1 parent d6638b4 commit 0e5e497
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/player.cpp
Expand Up @@ -240,6 +240,7 @@ void Player::deSerialize(std::istream &is, std::string playername)

u32 Player::addHud(HudElement *toadd)
{
JMutexAutoLock lock(m_mutex);
u32 id = getFreeHudID();

if (id < hud.size())
Expand All @@ -252,6 +253,8 @@ u32 Player::addHud(HudElement *toadd)

HudElement* Player::getHud(u32 id)
{
JMutexAutoLock lock(m_mutex);

if (id < hud.size())
return hud[id];

Expand All @@ -260,6 +263,8 @@ HudElement* Player::getHud(u32 id)

HudElement* Player::removeHud(u32 id)
{
JMutexAutoLock lock(m_mutex);

HudElement* retval = NULL;
if (id < hud.size()) {
retval = hud[id];
Expand All @@ -270,6 +275,8 @@ HudElement* Player::removeHud(u32 id)

void Player::clearHud()
{
JMutexAutoLock lock(m_mutex);

while(!hud.empty()) {
delete hud.back();
hud.pop_back();
Expand Down
9 changes: 8 additions & 1 deletion src/player.h
Expand Up @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_bloated.h"
#include "inventory.h"
#include "constants.h" // BS
#include "jthread/jmutexautolock.h"
#include <list>

#define PLAYERNAME_SIZE 20
Expand Down Expand Up @@ -202,7 +203,8 @@ class Player
return m_collisionbox;
}

u32 getFreeHudID() const {
u32 getFreeHudID() {
JMutexAutoLock lock(m_mutex);
size_t size = hud.size();
for (size_t i = 0; i != size; i++) {
if (!hud[i])
Expand Down Expand Up @@ -318,6 +320,11 @@ class Player
bool m_dirty;

std::vector<HudElement *> hud;
private:
// Protect some critical areas
// hud for example can be modified by EmergeThread
// and ServerThread
JMutex m_mutex;
};


Expand Down

0 comments on commit 0e5e497

Please sign in to comment.