From a1b93c397c8776e1bb07e3e01110d0ee5b9d31b9 Mon Sep 17 00:00:00 2001 From: Konstantin Kowalski Date: Fri, 26 Jul 2013 16:16:58 -0400 Subject: took out several strlen() calls out of the loop; replaced int's with size_t where needed --- other/DHT_bootstrap.c | 8 +++++--- testing/DHT_cryptosendfiletest.c | 11 ++++++----- testing/DHT_test.c | 11 ++++++----- testing/Messenger_test.c | 8 ++++---- testing/nTox.c | 24 +++++++++++++++--------- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 9d94fecc..8942c237 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -41,12 +41,14 @@ #define PORT 33445 +//TODO: rewrite unsigned char * hex_string_to_bin(char hex_string[]) { - unsigned char * val = malloc(strlen(hex_string)); + size_t len = strlen(hex_string); + unsigned char * val = malloc(len); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) + while(i < len) { sscanf(pos,"%2hhx",&val[i]); pos+=2; @@ -139,4 +141,4 @@ int main(int argc, char *argv[]) } shutdown_networking(); return 0; -} \ No newline at end of file +} diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index b06ddea5..5c3a0256 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c @@ -60,14 +60,15 @@ void printip(IP_Port ip_port) printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); } -/* horrible function from one of my first C programs. - *only here because I was too lazy to write a proper one. */ +//TODO: rewrite unsigned char * hex_string_to_bin(char hex_string[]) { - unsigned char * val = malloc(strlen(hex_string)); + size_t len = strlen(hex_string); + unsigned char * val = malloc(len); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) { + while(i < len) + { sscanf(pos,"%2hhx",&val[i]); pos+=2; i++; @@ -239,4 +240,4 @@ int main(int argc, char *argv[]) shutdown_networking(); return 0; -} \ No newline at end of file +} diff --git a/testing/DHT_test.c b/testing/DHT_test.c index bb482595..d14e1577 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -114,14 +114,15 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) printf("\n--------------------END-----------------------------\n\n\n"); } -//horrible function from one of my first C programs. -//only here because I was too lazy to write a proper one. +//TODO: rewrite unsigned char * hex_string_to_bin(char hex_string[]) { - unsigned char * val = malloc(strlen(hex_string)); + size_t len = strlen(hex_string); + unsigned char * val = malloc(len); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) { + while(i < len) + { sscanf(pos,"%2hhx",&val[i]); pos+=2; i++; @@ -191,4 +192,4 @@ int main(int argc, char *argv[]) shutdown_networking(); return 0; -} \ No newline at end of file +} diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 19ab4cc3..f38eb962 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -50,14 +50,14 @@ #endif -//horrible function from one of my first C programs. -//only here because I was too lazy to write a proper one. +//TODO: rewrite unsigned char * hex_string_to_bin(char hex_string[]) { - unsigned char * val = malloc(strlen(hex_string)); + size_t len = strlen(hex_string); + unsigned char * val = malloc(len); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) + while(i < len) { sscanf(pos,"%2hhx",&val[i]); pos+=2; diff --git a/testing/nTox.c b/testing/nTox.c index caa7d45c..3a5e9bb3 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -46,12 +46,15 @@ void new_lines(char *line) do_refresh(); } +//TODO: rewrite unsigned char * hex_string_to_bin(char hex_string[]) { - unsigned char * val = malloc(strlen(hex_string)); + size_t len = strlen(hex_string); + unsigned char * val = malloc(len); char * pos = hex_string; int i=0; - while(i < strlen(hex_string)) { + while(i < len) + { sscanf(pos,"%2hhx",&val[i]); pos+=2; i++; @@ -80,7 +83,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) doMessenger(); } else if (line[1] == 'm') { //message command: /m friendnumber messsage int i; - int len = strlen(line); + size_t len = strlen(line); char numstring[len-3]; char message[len-3]; for (i=0; i %s", friendnumber, temp, name, string); // someone please fix this new_lines(msg); -- cgit v1.2.3