From 58a6240af96ad6f699b97da2ea13bef943ee4431 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 13 Jul 2017 18:29:02 -0500 Subject: [PATCH] Fixed bug on odd byte ip addresses IP addresses that when converted to an integer ended up as a string with an odd number of bytes would result in a memory read into invalid memory. Zero filled the text string representation of the bytes to avoid this issue. --- test_multiaddr.h | 21 +++++++++++++++++++++ testing.c | 6 ++++-- varhexutils.c | 22 +++++++++++++++++----- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/test_multiaddr.h b/test_multiaddr.h index f98eff6..d8b7096 100644 --- a/test_multiaddr.h +++ b/test_multiaddr.h @@ -3,6 +3,27 @@ #include "multiaddr/multiaddr.h" #include "multiaddr/varhexutils.h" +int test_new_like_libp2p() { + int retVal = 0; + char* ip = "10.211.55.2"; + int port = 4001; + char str[strlen(ip) + 50]; + sprintf(str, "/ip4/%s/tcp/%d/", ip, port); + struct MultiAddress* ma_string = multiaddress_new_from_string(str); + // convert to binary + struct MultiAddress* ma_binary = multiaddress_new_from_bytes(ma_string->bytes, ma_string->bsize); + if (strcmp(ma_string->string, ma_binary->string) != 0) { + fprintf(stderr, "%s does not equal %s\n", ma_string->string, ma_binary->string); + goto exit; + } + + retVal = 1; + exit: + multiaddress_free(ma_string); + multiaddress_free(ma_binary); + return retVal; +} + int test_new_from_string() { struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/"); printf("Number of Bytes: %lu, Bytes: ", a->bsize); diff --git a/testing.c b/testing.c index 35e5997..dc86df9 100644 --- a/testing.c +++ b/testing.c @@ -10,7 +10,8 @@ const char* names[] = { "test_multiaddr_utils", "test_multiaddr_peer_id", "test_multiaddr_get_peer_id", - "test_multiaddr_bytes" + "test_multiaddr_bytes", + "test_new_like_libp2p" }; int (*funcs[])(void) = { @@ -21,7 +22,8 @@ int (*funcs[])(void) = { test_multiaddr_utils, test_multiaddr_peer_id, test_multiaddr_get_peer_id, - test_multiaddr_bytes + test_multiaddr_bytes, + test_new_like_libp2p }; int testit(const char* name, int (*func)(void)) { diff --git a/varhexutils.c b/varhexutils.c index cec9c08..dab4cfa 100644 --- a/varhexutils.c +++ b/varhexutils.c @@ -51,14 +51,26 @@ uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t uvarint_decode32(TON32INPUT, 60, &varintdecode_032); return &varintdecode_032; } -// +/** + * Converts a 64 bit integer into a hex string + * @param int2hex the 64 bit integer + * @returns a hex representation as a string (leading zero if necessary) + */ char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX { - static char int2hex_result[800]="\0"; - memset(int2hex_result,0,sizeof(int2hex_result)); - sprintf (int2hex_result, "%02lX", int2hex); - return int2hex_result; + static char result[50]; + memset(result, 0, 50); + sprintf(result, "%02lX", int2hex); + int slen = strlen(result); + if (slen % 2 != 0) { + for(int i = slen; i >= 0; --i) { + result[i+1] = result[i]; + } + result[0] = '0'; + } + return result; } + uint64_t Hex_To_Int(char * hax) { char * hex = NULL;