From 5fad72b910fde1bf66fecf7728ac73354ab23a3b Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sun, 9 Feb 2014 00:58:15 -0500 Subject: Modified hex_string_to_bin --- testing/misc_tools.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'testing') diff --git a/testing/misc_tools.c b/testing/misc_tools.c index efe1a555..d0d80038 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -33,17 +33,22 @@ #include #endif // DEBUG -/* TODO: rewrite */ -unsigned char *hex_string_to_bin(char hex_string[]) +// You are responsible for freeing the return value! +uint8_t *hex_string_to_bin(char *hex_string) { - size_t i, len = strlen(hex_string); - unsigned char *val = malloc(len); + // byte is represented by exactly 2 hex digits, so lenth of binary string + // is half of that of the hex one. only hex string with even length + // valid. the more proper implementation would be to check if strlen(hex_string) + // is odd and return error code if it is. we assume strlen is even. if it's not + // then the last byte just won't be written in 'ret'. + size_t i, len = strlen(hex_string)/2; + uint8_t *ret = malloc(len); char *pos = hex_string; for (i = 0; i < len; ++i, pos += 2) - sscanf(pos, "%2hhx", &val[i]); + sscanf(pos, "%2hhx", &ret[i]); - return val; + return ret; } int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) -- cgit v1.2.3 From ecbceac3414bf851d166e8ce7dceaf1dae12bb08 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sun, 9 Feb 2014 00:59:03 -0500 Subject: Fixed memory leaks and removed repeated code --- auto_tests/messenger_test.c | 13 +------------ other/bootstrap_serverdaemon/tox_bootstrap_daemon.c | 5 +++-- other/fun/sign.c | 15 ++------------- testing/DHT_test.c | 6 ++++-- testing/Messenger_test.c | 10 ++++++---- testing/experiment/group_chats_test1.c | 21 +++++++-------------- testing/tox_sync.c | 5 ++++- 7 files changed, 27 insertions(+), 48 deletions(-) (limited to 'testing') diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index 5c8e242e..0393e6d9 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -14,6 +14,7 @@ #include "config.h" #endif +#include "../testing/misc_tools.c" // hex_string_to_bin #include "../toxcore/Messenger.h" #include "../toxcore/Lossless_UDP.h" #include @@ -41,18 +42,6 @@ int friend_id_num = 0; Messenger *m; -unsigned char *hex_string_to_bin(char hex_string[]) -{ - size_t i, len = strlen(hex_string); - unsigned char *val = calloc(1, len); - char *pos = hex_string; - - for (i = 0; i < len; ++i, pos += 2) - sscanf(pos, "%2hhx", &val[i]); - - return val; -} - START_TEST(test_m_sendmesage) { char *message = "h-hi :3"; diff --git a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c index f2c54bb1..7f1f652c 100644 --- a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c @@ -266,8 +266,9 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) goto next; } - const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), - hex_string_to_bin((char *)bs_public_key)); + uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key); + const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), bs_public_key_bin); + free(bs_public_key_bin); if (!address_resolved) { syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address); diff --git a/other/fun/sign.c b/other/fun/sign.c index 423d974a..eaea9d6a 100644 --- a/other/fun/sign.c +++ b/other/fun/sign.c @@ -17,19 +17,7 @@ */ #include #include - -unsigned char *hex_string_to_bin(char hex_string[]) -{ - size_t len = strlen(hex_string); - unsigned char *val = malloc(len); - char *pos = hex_string; - int i; - - for (i = 0; i < len; ++i, pos += 2) - sscanf(pos, "%2hhx", &val[i]); - - return val; -} +#include "../../testing/misc_tools.c" // hex_string_to_bin int load_file(char *filename, char **result) { @@ -90,6 +78,7 @@ int main(int argc, char *argv[]) unsigned long long smlen; char *sm = malloc(size + crypto_sign_ed25519_BYTES * 2); crypto_sign_ed25519(sm, &smlen, data, size, secret_key); + free(secret_key); if (smlen - size != crypto_sign_ed25519_BYTES) goto fail; diff --git a/testing/DHT_test.c b/testing/DHT_test.c index a8ad8426..5ddb5945 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -205,8 +205,10 @@ int main(int argc, char *argv[]) if (scanf("%s", temp_id) != 1) exit(0); - - DHT_addfriend(dht, hex_string_to_bin(temp_id)); + + uint8_t *bin_id = hex_string_to_bin(temp_id); + DHT_addfriend(dht, bin_id); + free(bin_id); perror("Initialization"); diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 969d6b1f..707bb8db 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -161,14 +161,16 @@ int main(int argc, char *argv[]) setname(m, (uint8_t *)"Anon", 5); - char temp_id[128]; + char temp_hex_id[128]; printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); - if (scanf("%s", temp_id) != 1) { + if (scanf("%s", temp_hex_id) != 1) { return 1; } - - int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); + + uint8_t *bin_id = hex_string_to_bin(temp_hex_id); + int num = m_addfriend(m, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); + free(bin_id); perror("Initialization"); diff --git a/testing/experiment/group_chats_test1.c b/testing/experiment/group_chats_test1.c index a74f808a..fc10ab64 100644 --- a/testing/experiment/group_chats_test1.c +++ b/testing/experiment/group_chats_test1.c @@ -1,4 +1,6 @@ #include "../../toxcore/group_chats.h" +#include "../misc_tools.c" // hex_string_to_bin + #define NUM_CHATS 8 #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) @@ -54,19 +56,6 @@ void print_group(Group_Chat *chat) } } -unsigned char *hex_string_to_bin(char hex_string[]) -{ - size_t len = strlen(hex_string); - unsigned char *val = malloc(len); - char *pos = hex_string; - int i; - - for (i = 0; i < len; ++i, pos += 2) - sscanf(pos, "%2hhx", &val[i]); - - return val; -} - void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) { printf("%u: %s | %u\n", peer_number, message, length); @@ -95,8 +84,12 @@ int main(int argc, char *argv[]) * bootstrap_ip_port.ip.c[2] = 0; * bootstrap_ip_port.ip.c[3] = 1; */ bootstrap_ip_port.ip.uint32 = inet_addr(argv[1]); + + uint8_t *bootstrap_id = hex_string_to_bin(argv[3]); - chat_bootstrap(chat, bootstrap_ip_port, hex_string_to_bin(argv[3])); + chat_bootstrap(chat, bootstrap_ip_port, bootstrap_id); + + free(bootstrap_id); while (1) { diff --git a/testing/tox_sync.c b/testing/tox_sync.c index ccb5d20d..5d38d2e5 100644 --- a/testing/tox_sync.c +++ b/testing/tox_sync.c @@ -221,6 +221,7 @@ int main(int argc, char *argv[]) uint16_t port = htons(atoi(argv[argvoffset + 2])); unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], ipv6enabled, port, binary_string); + free(binary_string); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); @@ -242,7 +243,9 @@ int main(int argc, char *argv[]) return 1; } - int num = tox_add_friend(tox, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); + uint8_t *bin_id = hex_string_to_bin(temp_id); + int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); + free(bin_id); if (num < 0) { printf("\nSomething went wrong when adding friend.\n"); -- cgit v1.2.3 From 3782213b6e2731a29110e940546aa8981efad08b Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sun, 9 Feb 2014 02:51:43 -0500 Subject: Fixed build --- testing/misc_tools.c | 1 + 1 file changed, 1 insertion(+) (limited to 'testing') diff --git a/testing/misc_tools.c b/testing/misc_tools.c index d0d80038..5e322a90 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -28,6 +28,7 @@ #include #include #include +#include #ifdef DEBUG #include -- cgit v1.2.3