From 1ce731471d604927a3358b78d34608168b08a9f8 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 9 Jul 2014 00:05:13 -0400 Subject: [PATCH] fix potential race conditions --- src/chat.c | 2 +- src/chat_commands.c | 2 +- src/dns.c | 20 +++++++++++--------- src/misc_tools.c | 4 ++-- src/misc_tools.h | 2 +- src/prompt.c | 4 ++-- src/toxic.c | 17 ++++++++++------- src/windows.c | 6 ++---- src/windows.h | 1 + 9 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/chat.c b/src/chat.c index 5ffeb1f..59a70c7 100644 --- a/src/chat.c +++ b/src/chat.c @@ -253,7 +253,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t /* holds the lone filename */ char filename_nopath[MAX_STR_SIZE]; - get_file_name(filename_nopath, pathname); + get_file_name(filename_nopath, sizeof(filename_nopath), pathname); int len = strlen(filename_nopath); snprintf(msg, sizeof(msg), "File transfer request for '%s' (%llu bytes).", filename_nopath, diff --git a/src/chat_commands.c b/src/chat_commands.c index ba0435e..8ea9c79 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -193,7 +193,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv fseek(file_to_send, 0, SEEK_SET); char filename[MAX_STR_SIZE]; - get_file_name(filename, path); + get_file_name(filename, sizeof(filename), path); int filenum = tox_new_file_sender(m, self->num, filesize, (const uint8_t *) filename, strlen(filename)); if (filenum == -1) { diff --git a/src/dns.c b/src/dns.c index ddc131b..09476f8 100644 --- a/src/dns.c +++ b/src/dns.c @@ -45,6 +45,8 @@ #define TOX_DNS3_TXT_PREFIX "v=tox3;id=" #define DNS3_KEY_SZ 32 +extern struct _Winthread Winthread; + /* TODO: process keys from key file instead of hard-coding like a noob */ static struct dns3_server { char *name; @@ -77,7 +79,6 @@ static struct _thread_data { static struct _dns_thread { pthread_t tid; - pthread_mutex_t lock; } dns_thread; @@ -86,9 +87,9 @@ static int dns_error(ToxWindow *self, char *errmsg) char msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "User lookup failed: %s", errmsg); - pthread_mutex_lock(&dns_thread.lock); + pthread_mutex_lock(&Winthread.lock); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); - pthread_mutex_unlock(&dns_thread.lock); + pthread_mutex_unlock(&Winthread.lock); return -1; } @@ -272,15 +273,15 @@ void *dns3_lookup_thread(void *data) memcpy(encrypted_id, ans_id + prfx_len, ans_len - prfx_len); - if (tox_decrypt_dns3_TXT(dns_obj, (uint8_t *) t_data.id_bin, (uint8_t *) encrypted_id, strlen(encrypted_id), - request_id) == -1) { + if (tox_decrypt_dns3_TXT(dns_obj, (uint8_t *) t_data.id_bin, (uint8_t *) encrypted_id, + strlen(encrypted_id), request_id) == -1) { dns_error(self, "Core failed to decrypt DNS response."); kill_dns_thread(dns_obj); } - pthread_mutex_lock(&dns_thread.lock); + pthread_mutex_lock(&Winthread.lock); cmd_add_helper(self, t_data.m, t_data.id_bin, t_data.msg); - pthread_mutex_unlock(&dns_thread.lock); + pthread_mutex_unlock(&Winthread.lock); kill_dns_thread(dns_obj); return 0; @@ -302,9 +303,10 @@ void dns3_lookup(ToxWindow *self, Tox *m, char *id_bin, char *addr, char *msg) t_data.m = m; t_data.busy = 1; + pthread_mutex_unlock(&Winthread.lock); + if (pthread_create(&dns_thread.tid, NULL, dns3_lookup_thread, NULL) != 0) exit_toxic_err("failed in dns3_lookup", FATALERR_THREAD_CREATE); - if (pthread_mutex_init(&dns_thread.lock, NULL) != 0) - exit_toxic_err("failed in dns3_lookup", FATALERR_MUTEX_INIT); + pthread_mutex_lock(&Winthread.lock); } diff --git a/src/misc_tools.c b/src/misc_tools.c index 49fcd47..7e2afcc 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -194,7 +194,7 @@ int valid_nick(char *nick) } /* gets base file name from path or original file name if no path is supplied */ -void get_file_name(char *namebuf, const char *pathname) +void get_file_name(char *namebuf, int bufsize, const char *pathname) { int idx = strlen(pathname) - 1; @@ -213,7 +213,7 @@ void get_file_name(char *namebuf, const char *pathname) filename = tmpname; } - snprintf(namebuf, sizeof(namebuf), "%s", filename); + snprintf(namebuf, bufsize, "%s", filename); } /* converts str to all lowercase */ diff --git a/src/misc_tools.h b/src/misc_tools.h index 8ef4535..72a39d4 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -80,7 +80,7 @@ int qsort_strcasecmp_hlpr(const void *nick1, const void *nick2); int valid_nick(char *nick); /* gets base file name from path or original file name if no path is supplied */ -void get_file_name(char *namebuf, const char *pathname); +void get_file_name(char *namebuf, int bufsize, const char *pathname); /* converts str to all lowercase */ void str_to_lower(char *str); diff --git a/src/prompt.c b/src/prompt.c index f8ecf02..552d6e1 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -259,7 +259,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) wprintw(statusbar->topline, " %s", statusbar->nick); wattroff(statusbar->topline, A_BOLD); } else { - wprintw(statusbar->topline, "[Offline]"); + wprintw(statusbar->topline, " [Offline]"); wattron(statusbar->topline, A_BOLD); wprintw(statusbar->topline, " %s ", statusbar->nick); wattroff(statusbar->topline, A_BOLD); @@ -368,7 +368,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) const char *toxic_ver = strtok(ver, "_"); if ( (!strcmp("Online", statusmsg) || !strncmp("Toxing on Toxic", statusmsg, 15)) && toxic_ver != NULL) { - snprintf(statusmsg, sizeof(statusmsg), "Toxing on Toxic v.%s", toxic_ver); + snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver); s_len = strlen(statusmsg); statusmsg[s_len] = '\0'; } diff --git a/src/toxic.c b/src/toxic.c index c148573..e5b6c10 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -80,10 +80,9 @@ struct arg_opts { struct _Winthread Winthread; struct user_settings *user_settings = NULL; -static bool sig_exit_toxic = false; static void catch_SIGINT(int sig) { - sig_exit_toxic = true; + Winthread.sig_exit_toxic = true; } static void flag_window_resize(int sig) @@ -459,9 +458,8 @@ static void do_toxic(Tox *m, ToxWindow *prompt) pthread_mutex_lock(&Winthread.lock); do_connection(m, prompt); do_file_senders(m); - pthread_mutex_unlock(&Winthread.lock); - tox_do(m); /* main tox-core loop */ + pthread_mutex_unlock(&Winthread.lock); } #define INACTIVE_WIN_REFRESH_RATE 10 @@ -482,6 +480,11 @@ void *thread_winref(void *data) refresh_inactive_windows(); draw_count = 0; } + + if (Winthread.sig_exit_toxic) { + pthread_mutex_lock(&Winthread.lock); + exit_toxic_success(m); + } } } @@ -648,13 +651,13 @@ int main(int argc, char *argv[]) uint64_t cur_time = get_unix_time(); if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) { + pthread_mutex_lock(&Winthread.lock); store_data(m, DATA_FILE); + pthread_mutex_unlock(&Winthread.lock); + last_save = cur_time; } - if (sig_exit_toxic) - exit_toxic_success(m); - usleep(40000); } diff --git a/src/windows.c b/src/windows.c index b7b9c10..fc608dc 100644 --- a/src/windows.c +++ b/src/windows.c @@ -105,8 +105,7 @@ void on_nickchange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t windows[i].onNickChange(&windows[i], m, friendnumber, (const char *) string, length); } - if (store_data(m, DATA_FILE)) - wprintw(prompt->window, "\nCould not store Tox data\n"); + store_data(m, DATA_FILE); } void on_statusmessagechange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t length, void *userdata) @@ -138,8 +137,7 @@ void on_friendadded(Tox *m, int32_t friendnumber, bool sort) windows[i].onFriendAdded(&windows[i], m, friendnumber, sort); } - if (store_data(m, DATA_FILE)) - wprintw(prompt->window, "\nCould not store Tox data\n"); + store_data(m, DATA_FILE); } void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length, diff --git a/src/windows.h b/src/windows.h index 97bba81..a75e265 100644 --- a/src/windows.h +++ b/src/windows.h @@ -66,6 +66,7 @@ enum { struct _Winthread { pthread_t tid; pthread_mutex_t lock; + bool sig_exit_toxic; bool flag_resize; };