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.
This commit is contained in:
parent
7c6bd1e36b
commit
58a6240af9
3 changed files with 42 additions and 7 deletions
|
@ -3,6 +3,27 @@
|
||||||
#include "multiaddr/multiaddr.h"
|
#include "multiaddr/multiaddr.h"
|
||||||
#include "multiaddr/varhexutils.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() {
|
int test_new_from_string() {
|
||||||
struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
|
struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
|
||||||
printf("Number of Bytes: %lu, Bytes: ", a->bsize);
|
printf("Number of Bytes: %lu, Bytes: ", a->bsize);
|
||||||
|
|
|
@ -10,7 +10,8 @@ const char* names[] = {
|
||||||
"test_multiaddr_utils",
|
"test_multiaddr_utils",
|
||||||
"test_multiaddr_peer_id",
|
"test_multiaddr_peer_id",
|
||||||
"test_multiaddr_get_peer_id",
|
"test_multiaddr_get_peer_id",
|
||||||
"test_multiaddr_bytes"
|
"test_multiaddr_bytes",
|
||||||
|
"test_new_like_libp2p"
|
||||||
};
|
};
|
||||||
|
|
||||||
int (*funcs[])(void) = {
|
int (*funcs[])(void) = {
|
||||||
|
@ -21,7 +22,8 @@ int (*funcs[])(void) = {
|
||||||
test_multiaddr_utils,
|
test_multiaddr_utils,
|
||||||
test_multiaddr_peer_id,
|
test_multiaddr_peer_id,
|
||||||
test_multiaddr_get_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)) {
|
int testit(const char* name, int (*func)(void)) {
|
||||||
|
|
|
@ -51,14 +51,26 @@ uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t
|
||||||
uvarint_decode32(TON32INPUT, 60, &varintdecode_032);
|
uvarint_decode32(TON32INPUT, 60, &varintdecode_032);
|
||||||
return &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
|
char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX
|
||||||
{
|
{
|
||||||
static char int2hex_result[800]="\0";
|
static char result[50];
|
||||||
memset(int2hex_result,0,sizeof(int2hex_result));
|
memset(result, 0, 50);
|
||||||
sprintf (int2hex_result, "%02lX", int2hex);
|
sprintf(result, "%02lX", int2hex);
|
||||||
return int2hex_result;
|
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)
|
uint64_t Hex_To_Int(char * hax)
|
||||||
{
|
{
|
||||||
char * hex = NULL;
|
char * hex = NULL;
|
||||||
|
|
Loading…
Reference in a new issue