From e7c7e4ea069e2d04b27bb815262ddcf07cd58c77 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Mon, 1 Jan 2024 23:42:22 -0500 Subject: [PATCH] Fix possible data race 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 --- src/game_base.c | 4 +++- src/windows.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/game_base.c b/src/game_base.c index 6ff9d40ef..c0801f888 100644 --- a/src/game_base.c +++ b/src/game_base.c @@ -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); @@ -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; } diff --git a/src/windows.c b/src/windows.c index ea15a8798..1c1b21d74 100644 --- a/src/windows.c +++ b/src/windows.c @@ -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; }