From b071a9e99215a66eb56b7bd721dd9a6c988aaf18 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 23 Sep 2014 22:51:56 -0400 Subject: [PATCH] more thorough error checking --- src/audio_call.c | 17 ++++++++++++----- src/chat.c | 7 ++++++- src/chat_commands.c | 21 +++++++++++++++++++-- src/device.c | 15 +++++++++++---- src/file_senders.c | 2 +- src/friendlist.c | 18 ++++++++++++++++-- src/groupchat.c | 5 +++++ src/log.c | 1 - src/notify.c | 5 ++++- src/toxic.c | 19 ++++++++++++++++--- src/toxic.h | 6 +++--- 11 files changed, 93 insertions(+), 23 deletions(-) diff --git a/src/audio_call.c b/src/audio_call.c index e5cf617..f49544e 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -63,19 +63,23 @@ typedef struct Call { } Call; -void set_call(Call* call, bool start) +static int set_call(Call* call, bool start) { call->in_idx = -1; call->out_idx = -1; if ( start ) { call->ttas = true; - pthread_mutex_init(&call->mutex, NULL); + if (pthread_mutex_init(&call->mutex, NULL) != 0) + return -1; } else { call->ttid = 0; - pthread_mutex_destroy(&call->mutex); + if (pthread_mutex_destroy(&call->mutex) != 0) + return -1; } + + return 0; } struct ASettings { @@ -199,7 +203,8 @@ int start_transmission(ToxWindow *self) !toxav_capability_supported(ASettins.av, self->call_idx, AudioEncoding) ) return -1; - set_call(&ASettins.calls[self->call_idx], true); + if (set_call(&ASettins.calls[self->call_idx], true) == -1) + return -1; ToxAvCSettings csettings; toxav_get_peer_csettings(ASettins.av, self->call_idx, 0, &csettings); @@ -234,7 +239,9 @@ int stop_transmission(int call_index) if ( ASettins.calls[call_index].out_idx != -1 ) close_device(output, ASettins.calls[call_index].out_idx); - set_call(&ASettins.calls[call_index], false); + if (set_call(&ASettins.calls[call_index], false) == -1) + return -1; + return 0; } diff --git a/src/chat.c b/src/chat.c index 81e659e..f69a53a 100644 --- a/src/chat.c +++ b/src/chat.c @@ -528,7 +528,12 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec uint64_t datapos; memcpy(&datapos, tmp, sizeof(uint64_t)); - fseek(fp, datapos, SEEK_SET); + if (fseek(fp, datapos, SEEK_SET) == -1) { + snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", filename); + close_file_sender(self, m, send_idx, NULL, TOX_FILECONTROL_FINISHED, filenum, num); + break; + } + tox_file_send_control(m, num, 0, filenum, TOX_FILECONTROL_ACCEPT, 0, 0); file_senders[send_idx].noconnection = false; break; diff --git a/src/chat_commands.c b/src/chat_commands.c index 7881d0f..6652833 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -224,9 +224,25 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv return; } - fseek(file_to_send, 0, SEEK_END); + if (fseek(file_to_send, 0, SEEK_END) == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); + fclose(file_to_send); + return; + } + uint64_t filesize = ftell(file_to_send); - fseek(file_to_send, 0, SEEK_SET); + + if (filesize == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); + fclose(file_to_send); + return; + } + + if (fseek(file_to_send, 0, SEEK_SET) == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); + fclose(file_to_send); + return; + } char filename[MAX_STR_SIZE] = {0}; get_file_name(filename, sizeof(filename), path); @@ -235,6 +251,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv if (filenum == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error sending file."); + fclose(file_to_send); return; } diff --git a/src/device.c b/src/device.c index e3ec4f4..74bf8c3 100644 --- a/src/device.c +++ b/src/device.c @@ -129,7 +129,8 @@ DeviceError init_devices() // Start poll thread - pthread_mutex_init(&mutex, NULL); + if (pthread_mutex_init(&mutex, NULL) != 0) + return de_InternalError; pthread_t thread_id; if ( pthread_create(&thread_id, NULL, thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) @@ -148,7 +149,8 @@ DeviceError terminate_devices() thread_running = false; usleep(20000); - pthread_mutex_destroy(&mutex); + if (pthread_mutex_destroy(&mutex) != 0) + return (DeviceError) de_InternalError; return (DeviceError) de_None; } @@ -239,7 +241,10 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx device->source = running[type][i]->source; } device->ref_count++; - pthread_mutex_init(device->mutex, NULL); + + if (pthread_mutex_init(device->mutex, NULL) != 0) + return de_InternalError; + unlock; return de_None; } @@ -291,7 +296,9 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx thread_paused = false; } - pthread_mutex_init(device->mutex, NULL); + if (pthread_mutex_init(device->mutex, NULL) != 0) + return de_InternalError; + unlock; return de_None; } diff --git a/src/file_senders.c b/src/file_senders.c index 6022886..eb12ae7 100644 --- a/src/file_senders.c +++ b/src/file_senders.c @@ -229,7 +229,7 @@ static void send_file_data(ToxWindow *self, Tox *m, int i, int32_t friendnum, in snprintf(msg, sizeof(msg), "File transfer for '%s' failed: Read error.", file_senders[i].filename); close_file_sender(self, m, i, msg, TOX_FILECONTROL_KILL, filenum, friendnum); sound_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, NULL); - + if (self->active_box != -1) box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", msg); else diff --git a/src/friendlist.c b/src/friendlist.c index 7fb4412..0b63257 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -163,6 +163,7 @@ static int save_blocklist(char *path) ret = 0; fclose(fp); + on_error: free(data); return ret; @@ -180,9 +181,22 @@ int load_blocklist(char *path) if (fp == NULL) return -1; - fseek(fp, 0, SEEK_END); + if (fseek(fp, 0L, SEEK_END) == -1) { + fclose(fp); + return -1; + } + int len = ftell(fp); - fseek(fp, 0, SEEK_SET); + + if (len == -1) { + fclose(fp); + return -1; + } + + if (fseek(fp, 0L, SEEK_SET) == -1) { + fclose(fp); + return -1; + } char *data = malloc(len); diff --git a/src/groupchat.c b/src/groupchat.c index 134b6bd..959ce5f 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -67,11 +67,16 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum) groupchats[i].chatwin = add_window(m, new_group_chat(m, groupnum)); groupchats[i].active = true; groupchats[i].num_peers = 0; + groupchats[i].peer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH); groupchats[i].oldpeer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH); groupchats[i].peer_name_lengths = malloc(sizeof(uint16_t)); groupchats[i].oldpeer_name_lengths = malloc(sizeof(uint16_t)); + if (groupchats[i].peer_names == NULL || groupchats[i].oldpeer_names == NULL + || groupchats[i].peer_name_lengths == NULL || groupchats[i].oldpeer_name_lengths == NULL) + exit_toxic_err("failed in init_groupchat_win", FATALERR_MEMORY); + memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME)); groupchats[i].oldpeer_name_lengths[0] = (uint16_t) strlen(UNKNOWN_NAME); diff --git a/src/log.c b/src/log.c index 5c1a3af..0942b4b 100644 --- a/src/log.c +++ b/src/log.c @@ -90,7 +90,6 @@ static int init_logging_session(char *name, const char *selfkey, const char *oth free(user_config_dir); log->file = fopen(log_path, "a+"); - snprintf(log->path, sizeof(log->path), "%s", log_path); if (log->file == NULL) diff --git a/src/notify.c b/src/notify.c index 9274186..c2d1eca 100644 --- a/src/notify.c +++ b/src/notify.c @@ -328,8 +328,11 @@ int init_notify(int login_cooldown, int notification_timeout) #endif /* SOUND_NOTIFY */ #if defined(SOUND_NOTIFY) || defined(BOX_NOTIFY) - pthread_mutex_init(Control.poll_mutex, NULL); + if (pthread_mutex_init(Control.poll_mutex, NULL) != 0) + return -1; + pthread_t thread; + if (pthread_create(&thread, NULL, do_playing, NULL) != 0 || pthread_detach(thread) != 0 ) { pthread_mutex_destroy(Control.poll_mutex); return -1; diff --git a/src/toxic.c b/src/toxic.c index 492ee19..0ffd8be 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -629,9 +629,22 @@ static void load_data(Tox *m, char *path) FILE *fd; if ((fd = fopen(path, "rb")) != NULL) { - fseek(fd, 0, SEEK_END); + if (fseek(fd, 0L, SEEK_END) == -1) { + fclose(fd); + exit_toxic_err("failed in load_data", FATALERR_FILEOP); + } + int len = ftell(fd); - fseek(fd, 0, SEEK_SET); + + if (len == -1) { + fclose(fd); + exit_toxic_err("failed in load_data", FATALERR_FILEOP); + } + + if (fseek(fd, 0L, SEEK_SET)) { + fclose(fd); + exit_toxic_err("failed in load_data", FATALERR_FILEOP); + } char *buf = malloc(len); @@ -643,7 +656,7 @@ static void load_data(Tox *m, char *path) if (fread(buf, len, 1, fd) != 1) { free(buf); fclose(fd); - exit_toxic_err("failed in load_data", FATALERR_FREAD); + exit_toxic_err("failed in load_data", FATALERR_FILEOP); } bool is_encrypted = tox_is_data_encrypted((uint8_t *) buf); diff --git a/src/toxic.h b/src/toxic.h index 093aee0..709a715 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -69,9 +69,9 @@ typedef enum _FATAL_ERRS { FATALERR_MEMORY = -1, /* heap memory allocation failed */ - FATALERR_FREAD = -2, /* fread() failed on critical read */ - FATALERR_THREAD_CREATE = -3, /* thread creation failed */ - FATALERR_MUTEX_INIT = -4, /* mutex init failed */ + FATALERR_FILEOP = -2, /* critical file operation failed */ + FATALERR_THREAD_CREATE = -3, /* thread creation failed for critical thread */ + FATALERR_MUTEX_INIT = -4, /* mutex init for critical thread failed */ FATALERR_THREAD_ATTR = -5, /* thread attr object init failed */ FATALERR_LOCALE_SET = -6, /* system locale not set */ FATALERR_STORE_DATA = -7, /* store_data failed in critical section */