From 43f66085304190651b60a6f09c3b9dab8279e607 Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Sat, 3 Aug 2013 17:57:44 -0400 Subject: cleaned up load_key() and added more error checking --- testing/nTox.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 15e209a9..3aea6b32 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -361,16 +361,18 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) void load_key(char *path) { FILE *data_file = fopen(path, "r"); + int size = 0; if (data_file) { //load keys fseek(data_file, 0, SEEK_END); - int size = ftell(data_file); - fseek(data_file, 0, SEEK_SET); + size = ftell(data_file); + rewind(data_file); + uint8_t data[size]; if (fread(data, sizeof(uint8_t), size, data_file) != size){ - printf("[i] could not read data file\n[i] exiting\n"); - exit(1); + fputs("[!] could not read data file! exiting...\n", stderr); + goto FILE_ERROR; } Messenger_load(data, size); @@ -387,11 +389,15 @@ void load_key(char *path) } if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ - puts("[i] could not write data file! exiting..."); - exit(1); + fputs("[!] could not write data file! exiting...", stderr); + goto FILE_ERROR; } } - fclose(data_file); + +FILE_ERROR: + if(fclose(data_file) < 0) + perror("[!] fclose failed"); + exit(1); } void print_help(void) -- cgit v1.2.3 From 6222e32848b7468c17d8d9caef4845bc600bbced Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Sat, 3 Aug 2013 18:02:09 -0400 Subject: added some better error checking for print_nickname() anr print_statuschange() --- testing/nTox.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 3aea6b32..52d1bfd1 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -343,19 +343,21 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length) void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; - getname(friendnumber, (uint8_t*)name); - char msg[100+length]; - sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); - new_lines(msg); + if(getname(friendnumber, (uint8_t*)name) != -1) { + char msg[100+length]; + sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); + new_lines(msg); + } } void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; - getname(friendnumber, (uint8_t*)name); - char msg[100+length+strlen(name)+1]; - sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); - new_lines(msg); + if(getname(friendnumber, (uint8_t*)name) != -1) { + char msg[100+length+strlen(name)+1]; + sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); + new_lines(msg); + } } void load_key(char *path) -- cgit v1.2.3 From 175a434de3cd69f7bfa97fac9ec6f080663a6f9d Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Sat, 3 Aug 2013 18:06:28 -0400 Subject: removed some magic numbers from line_eval() --- testing/nTox.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 52d1bfd1..630c5f8e 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -145,19 +145,19 @@ void line_eval(char *line) int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); char numstring[100]; switch (num) { - case -1: + case FAERR_TOOLONG: sprintf(numstring, "[i] Message is too long."); break; - case -2: + case FAERR_NOMESSAGE: sprintf(numstring, "[i] Please add a message to your request."); break; - case -3: + case FAERR_OWNKEY: sprintf(numstring, "[i] That appears to be your own ID."); break; - case -4: + case FAERR_ALREADYSENT: sprintf(numstring, "[i] Friend request already sent."); break; - case -5: + case FAERR_UNKNOWN: sprintf(numstring, "[i] Undefined error when adding friend."); break; default: -- cgit v1.2.3 From b36bca295c296f886872df98de388c8c5c5ad177 Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Sat, 3 Aug 2013 21:59:17 -0400 Subject: fixed bug that would cause this to always exit --- testing/nTox.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/nTox.c b/testing/nTox.c index 630c5f8e..4be69ac4 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -396,6 +396,8 @@ void load_key(char *path) } } + return; + FILE_ERROR: if(fclose(data_file) < 0) perror("[!] fclose failed"); -- cgit v1.2.3