Skip to content
Snippets Groups Projects
Commit 0e5e4973 authored by Loïc Blot's avatar Loïc Blot
Browse files

Protect Player::hud from concurrent modifications

Sometimes HUD can be modified by ServerThread and EmergeThread results in a crash on client side because the HUD is not correct
parent d6638b43
No related branches found
No related tags found
No related merge requests found
...@@ -240,6 +240,7 @@ void Player::deSerialize(std::istream &is, std::string playername) ...@@ -240,6 +240,7 @@ void Player::deSerialize(std::istream &is, std::string playername)
u32 Player::addHud(HudElement *toadd) u32 Player::addHud(HudElement *toadd)
{ {
JMutexAutoLock lock(m_mutex);
u32 id = getFreeHudID(); u32 id = getFreeHudID();
if (id < hud.size()) if (id < hud.size())
...@@ -252,6 +253,8 @@ u32 Player::addHud(HudElement *toadd) ...@@ -252,6 +253,8 @@ u32 Player::addHud(HudElement *toadd)
HudElement* Player::getHud(u32 id) HudElement* Player::getHud(u32 id)
{ {
JMutexAutoLock lock(m_mutex);
if (id < hud.size()) if (id < hud.size())
return hud[id]; return hud[id];
...@@ -260,6 +263,8 @@ HudElement* Player::getHud(u32 id) ...@@ -260,6 +263,8 @@ HudElement* Player::getHud(u32 id)
HudElement* Player::removeHud(u32 id) HudElement* Player::removeHud(u32 id)
{ {
JMutexAutoLock lock(m_mutex);
HudElement* retval = NULL; HudElement* retval = NULL;
if (id < hud.size()) { if (id < hud.size()) {
retval = hud[id]; retval = hud[id];
...@@ -270,6 +275,8 @@ HudElement* Player::removeHud(u32 id) ...@@ -270,6 +275,8 @@ HudElement* Player::removeHud(u32 id)
void Player::clearHud() void Player::clearHud()
{ {
JMutexAutoLock lock(m_mutex);
while(!hud.empty()) { while(!hud.empty()) {
delete hud.back(); delete hud.back();
hud.pop_back(); hud.pop_back();
......
...@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_bloated.h" #include "irrlichttypes_bloated.h"
#include "inventory.h" #include "inventory.h"
#include "constants.h" // BS #include "constants.h" // BS
#include "jthread/jmutexautolock.h"
#include <list> #include <list>
#define PLAYERNAME_SIZE 20 #define PLAYERNAME_SIZE 20
...@@ -202,7 +203,8 @@ class Player ...@@ -202,7 +203,8 @@ class Player
return m_collisionbox; return m_collisionbox;
} }
u32 getFreeHudID() const { u32 getFreeHudID() {
JMutexAutoLock lock(m_mutex);
size_t size = hud.size(); size_t size = hud.size();
for (size_t i = 0; i != size; i++) { for (size_t i = 0; i != size; i++) {
if (!hud[i]) if (!hud[i])
...@@ -318,6 +320,11 @@ class Player ...@@ -318,6 +320,11 @@ class Player
bool m_dirty; bool m_dirty;
std::vector<HudElement *> hud; std::vector<HudElement *> hud;
private:
// Protect some critical areas
// hud for example can be modified by EmergeThread
// and ServerThread
JMutex m_mutex;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment