Skip to content

Commit

Permalink
Fix possible data race
Browse files Browse the repository at this point in the history
Exiting a game will touch the global windows array and send a packet
in the case of multiplayer games, both of which need to be
protected by a mutex
  • Loading branch information
JFreegman committed Jan 2, 2024
1 parent d95b2f9 commit e7c7e4e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/game_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ int game_initialize(const ToxWindow *parent, Tox *m, GameType type, uint32_t id,
return -4;
}

int init_ret = game_initialize_type(game, multiplayer_data, length, self_host);
const int init_ret = game_initialize_type(game, multiplayer_data, length, self_host);

if (init_ret < 0) {
game_init_abort(parent, self);
Expand Down Expand Up @@ -785,7 +785,9 @@ bool game_onKey(ToxWindow *self, Tox *m, wint_t key, bool is_printable)
GameData *game = self->game;

if (key == KEY_F(9)) {
pthread_mutex_lock(&Winthread.lock);
game_kill(self);
pthread_mutex_unlock(&Winthread.lock);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ void draw_active_window(Tox *m)
set_next_window(ch);
}

a->onKey(a, m, ch, false);
a->onKey(a, m, ch, false); // we lock only when necessary in the onKey callback

return;
}
Expand Down

0 comments on commit e7c7e4e

Please sign in to comment.