1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 02:23:02 +01:00

more thorough error checking

This commit is contained in:
Jfreegman 2014-09-23 22:51:56 -04:00
parent 48cf4ebf02
commit b071a9e992
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
11 changed files with 93 additions and 23 deletions

View File

@ -63,19 +63,23 @@ typedef struct Call {
} Call; } Call;
void set_call(Call* call, bool start) static int set_call(Call* call, bool start)
{ {
call->in_idx = -1; call->in_idx = -1;
call->out_idx = -1; call->out_idx = -1;
if ( start ) { if ( start ) {
call->ttas = true; call->ttas = true;
pthread_mutex_init(&call->mutex, NULL); if (pthread_mutex_init(&call->mutex, NULL) != 0)
return -1;
} }
else { else {
call->ttid = 0; call->ttid = 0;
pthread_mutex_destroy(&call->mutex); if (pthread_mutex_destroy(&call->mutex) != 0)
return -1;
} }
return 0;
} }
struct ASettings { struct ASettings {
@ -199,7 +203,8 @@ int start_transmission(ToxWindow *self)
!toxav_capability_supported(ASettins.av, self->call_idx, AudioEncoding) ) !toxav_capability_supported(ASettins.av, self->call_idx, AudioEncoding) )
return -1; return -1;
set_call(&ASettins.calls[self->call_idx], true); if (set_call(&ASettins.calls[self->call_idx], true) == -1)
return -1;
ToxAvCSettings csettings; ToxAvCSettings csettings;
toxav_get_peer_csettings(ASettins.av, self->call_idx, 0, &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 ) if ( ASettins.calls[call_index].out_idx != -1 )
close_device(output, ASettins.calls[call_index].out_idx); 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; return 0;
} }

View File

@ -528,7 +528,12 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec
uint64_t datapos; uint64_t datapos;
memcpy(&datapos, tmp, sizeof(uint64_t)); 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); tox_file_send_control(m, num, 0, filenum, TOX_FILECONTROL_ACCEPT, 0, 0);
file_senders[send_idx].noconnection = false; file_senders[send_idx].noconnection = false;
break; break;

View File

@ -224,9 +224,25 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
return; 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); 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}; char filename[MAX_STR_SIZE] = {0};
get_file_name(filename, sizeof(filename), path); 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) { if (filenum == -1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error sending file."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error sending file.");
fclose(file_to_send);
return; return;
} }

View File

@ -129,7 +129,8 @@ DeviceError init_devices()
// Start poll thread // Start poll thread
pthread_mutex_init(&mutex, NULL); if (pthread_mutex_init(&mutex, NULL) != 0)
return de_InternalError;
pthread_t thread_id; pthread_t thread_id;
if ( pthread_create(&thread_id, NULL, thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) 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; thread_running = false;
usleep(20000); usleep(20000);
pthread_mutex_destroy(&mutex); if (pthread_mutex_destroy(&mutex) != 0)
return (DeviceError) de_InternalError;
return (DeviceError) de_None; 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->source = running[type][i]->source;
} }
device->ref_count++; device->ref_count++;
pthread_mutex_init(device->mutex, NULL);
if (pthread_mutex_init(device->mutex, NULL) != 0)
return de_InternalError;
unlock; unlock;
return de_None; return de_None;
} }
@ -291,7 +296,9 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx
thread_paused = false; thread_paused = false;
} }
pthread_mutex_init(device->mutex, NULL); if (pthread_mutex_init(device->mutex, NULL) != 0)
return de_InternalError;
unlock; unlock;
return de_None; return de_None;
} }

View File

@ -163,6 +163,7 @@ static int save_blocklist(char *path)
ret = 0; ret = 0;
fclose(fp); fclose(fp);
on_error: on_error:
free(data); free(data);
return ret; return ret;
@ -180,9 +181,22 @@ int load_blocklist(char *path)
if (fp == NULL) if (fp == NULL)
return -1; return -1;
fseek(fp, 0, SEEK_END); if (fseek(fp, 0L, SEEK_END) == -1) {
fclose(fp);
return -1;
}
int len = ftell(fp); 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); char *data = malloc(len);

View File

@ -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].chatwin = add_window(m, new_group_chat(m, groupnum));
groupchats[i].active = true; groupchats[i].active = true;
groupchats[i].num_peers = 0; groupchats[i].num_peers = 0;
groupchats[i].peer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH); 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].oldpeer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH);
groupchats[i].peer_name_lengths = malloc(sizeof(uint16_t)); groupchats[i].peer_name_lengths = malloc(sizeof(uint16_t));
groupchats[i].oldpeer_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)); memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME));
groupchats[i].oldpeer_name_lengths[0] = (uint16_t) strlen(UNKNOWN_NAME); groupchats[i].oldpeer_name_lengths[0] = (uint16_t) strlen(UNKNOWN_NAME);

View File

@ -90,7 +90,6 @@ static int init_logging_session(char *name, const char *selfkey, const char *oth
free(user_config_dir); free(user_config_dir);
log->file = fopen(log_path, "a+"); log->file = fopen(log_path, "a+");
snprintf(log->path, sizeof(log->path), "%s", log_path); snprintf(log->path, sizeof(log->path), "%s", log_path);
if (log->file == NULL) if (log->file == NULL)

View File

@ -328,8 +328,11 @@ int init_notify(int login_cooldown, int notification_timeout)
#endif /* SOUND_NOTIFY */ #endif /* SOUND_NOTIFY */
#if defined(SOUND_NOTIFY) || defined(BOX_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; pthread_t thread;
if (pthread_create(&thread, NULL, do_playing, NULL) != 0 || pthread_detach(thread) != 0 ) { if (pthread_create(&thread, NULL, do_playing, NULL) != 0 || pthread_detach(thread) != 0 ) {
pthread_mutex_destroy(Control.poll_mutex); pthread_mutex_destroy(Control.poll_mutex);
return -1; return -1;

View File

@ -629,9 +629,22 @@ static void load_data(Tox *m, char *path)
FILE *fd; FILE *fd;
if ((fd = fopen(path, "rb")) != NULL) { 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); 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); char *buf = malloc(len);
@ -643,7 +656,7 @@ static void load_data(Tox *m, char *path)
if (fread(buf, len, 1, fd) != 1) { if (fread(buf, len, 1, fd) != 1) {
free(buf); free(buf);
fclose(fd); 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); bool is_encrypted = tox_is_data_encrypted((uint8_t *) buf);

View File

@ -69,9 +69,9 @@
typedef enum _FATAL_ERRS { typedef enum _FATAL_ERRS {
FATALERR_MEMORY = -1, /* heap memory allocation failed */ FATALERR_MEMORY = -1, /* heap memory allocation failed */
FATALERR_FREAD = -2, /* fread() failed on critical read */ FATALERR_FILEOP = -2, /* critical file operation failed */
FATALERR_THREAD_CREATE = -3, /* thread creation failed */ FATALERR_THREAD_CREATE = -3, /* thread creation failed for critical thread */
FATALERR_MUTEX_INIT = -4, /* mutex init failed */ FATALERR_MUTEX_INIT = -4, /* mutex init for critical thread failed */
FATALERR_THREAD_ATTR = -5, /* thread attr object init failed */ FATALERR_THREAD_ATTR = -5, /* thread attr object init failed */
FATALERR_LOCALE_SET = -6, /* system locale not set */ FATALERR_LOCALE_SET = -6, /* system locale not set */
FATALERR_STORE_DATA = -7, /* store_data failed in critical section */ FATALERR_STORE_DATA = -7, /* store_data failed in critical section */