From 0b8789ebbcdb30d4614b22fdc6c93c9caed42cc9 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 20 Feb 2017 17:45:48 -0500 Subject: [PATCH] Bug fixes --- multiaddr.c | 2 +- test_multiaddr.h | 22 ++++++++++++++++++++-- testing.c | 8 ++++++-- varhexutils.c | 14 ++++++-------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/multiaddr.c b/multiaddr.c index c93a09d..ce89a3d 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -72,7 +72,7 @@ struct MultiAddress* multiaddress_new_from_string(const char* straddress)//Const } strcpy(out->string, straddress); - if (string_to_bytes(&out->bytes, &out->bsize, out->string, strlen(out->string)) == 0 ) + if (string_to_bytes(&(out->bytes), &out->bsize, out->string, strlen(out->string)) == 0 ) { multiaddress_free(out); return NULL; diff --git a/test_multiaddr.h b/test_multiaddr.h index dc03cc4..3afea04 100644 --- a/test_multiaddr.h +++ b/test_multiaddr.h @@ -42,7 +42,25 @@ int test_full() { return 1; } -int test_hex_to_val() { - return 0; +int test_hex_to_var() { + size_t d; + unsigned char* result = Hex_To_Var("04", &d); + if (d != 1) + return 0; + if (result[0] != 4) + return 0; + + if (result != NULL) + free(result); + return 1; +} + +int test_int_to_hex() { + int val = 2555351; + char* result = Int_To_Hex(val); + int retVal = Hex_To_Int(result); + if (retVal != val) + return 0; + return 1; } diff --git a/testing.c b/testing.c index 8efdb68..7c7bd48 100644 --- a/testing.c +++ b/testing.c @@ -4,12 +4,16 @@ const char* names[] = { "test_new_from_string", - "test_full" + "test_full", + "test_hex_to_var", + "test_int_to_hex" }; int (*funcs[])(void) = { test_new_from_string, - test_full + test_full, + test_hex_to_var, + test_int_to_hex }; int testit(const char* name, int (*func)(void)) { diff --git a/varhexutils.c b/varhexutils.c index 6c40ee1..b6c247a 100644 --- a/varhexutils.c +++ b/varhexutils.c @@ -132,17 +132,15 @@ unsigned char* Hex_To_Var(const char* incoming, size_t* num_bytes) //HEX TO VAR[ retVal = (unsigned char*)malloc(*num_bytes); char code[3]; - code[3]='\0'; - int i=0; - for(i=0; i < incoming_size; i += 2) + code[2]='\0'; + int pos = 0; + for(int i=0; i < incoming_size; i += 2) { code[0] = incoming[i]; code[1] = incoming[i+1]; - char *ck = NULL; - uint64_t lu = 0; - lu=strtoul(code, &ck, 16); - retVal[i] = lu; - i++; + uint64_t lu = strtol(code, NULL, 16); + retVal[pos] = (unsigned int)lu; + pos++; } return retVal; }