From 603ed1d72f6561ed97ad134c84bff3961575fdac Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 17 Apr 2017 11:56:36 -0500 Subject: [PATCH] Fixed multiaddress_get_peer_id to return allocated memory Returing in place causes problems with continuations i.e. ending slashes. Therefore it is better to return allocated memory. --- include/multiaddr/base58.h | 8 ------- include/multiaddr/multiaddr.h | 2 +- multiaddr.c | 26 +++++++++++++++++++---- test_multiaddr.h | 39 ++++++++++++++++++++++++++++++++--- testing.c | 2 ++ 5 files changed, 61 insertions(+), 16 deletions(-) diff --git a/include/multiaddr/base58.h b/include/multiaddr/base58.h index 874a631..1b10a7c 100644 --- a/include/multiaddr/base58.h +++ b/include/multiaddr/base58.h @@ -1,11 +1,3 @@ -// -// base58.h -// libp2p_xcode -// -// Created by John Jones on 11/7/16. -// Copyright © 2016 JMJAtlanta. All rights reserved. -// - #ifndef base58_h #define base58_h #include "varint.h" diff --git a/include/multiaddr/multiaddr.h b/include/multiaddr/multiaddr.h index 4a412ef..59d91da 100644 --- a/include/multiaddr/multiaddr.h +++ b/include/multiaddr/multiaddr.h @@ -83,7 +83,7 @@ int multiaddress_get_ip_port(const struct MultiAddress* in); /** * Pulls the peer ID out of a multiaddress struct * @param in the MultiAddress - * @returns a pointer to the peer id string, or NULL + * @returns the peer id string, or NULL NOTE: This allocates memory that needs to be freed */ char* multiaddress_get_peer_id(const struct MultiAddress* in); diff --git a/multiaddr.c b/multiaddr.c index f99ce3d..aad41eb 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -159,10 +159,28 @@ int multiaddress_get_ip_port(const struct MultiAddress* in) { } char* multiaddress_get_peer_id(const struct MultiAddress* in) { - char* ptr = strstr(in->string, "/ipfs/"); - if (ptr == NULL) - return NULL; - return &ptr[6]; + char* result = NULL; + int str_len = 0; + char* slash = NULL; + char* ptr = NULL; + + ptr = strstr(in->string, "/ipfs/"); + if (ptr != NULL && ptr[6] != 0) { + ptr += 6; + str_len = strlen(ptr); + slash = strchr(ptr, '/'); + if (slash != NULL) { + str_len = slash - ptr; + } + if (str_len > 0) { + result = malloc(str_len + 1); + if (result != NULL) { + memset(result, 0, str_len); + memcpy(result, ptr, str_len); + } + } + } + return result; } void multiaddress_free(struct MultiAddress* in) { diff --git a/test_multiaddr.h b/test_multiaddr.h index 613fef2..3751e33 100644 --- a/test_multiaddr.h +++ b/test_multiaddr.h @@ -114,7 +114,7 @@ int test_multiaddr_peer_id() { char full_string[255]; char* result = NULL; char* bytes = NULL; - int retVal = 0; + int retVal = 0, port = 0; struct MultiAddress *addr = NULL, *addr2 = NULL; sprintf(full_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s/", orig_address); @@ -123,9 +123,10 @@ int test_multiaddr_peer_id() { result = multiaddress_get_peer_id(addr); - if (result == NULL || strncmp(result, orig_address, strlen(orig_address)) != 0) + if (result == NULL || strcmp(result, orig_address) != 0) goto exit; + free(result); result = NULL; // switch to bytes and back again to verify the peer id follows... @@ -155,12 +156,20 @@ int test_multiaddr_peer_id() { goto exit; } - int port = multiaddress_get_ip_port(addr2); + port = multiaddress_get_ip_port(addr2); if (port != 4001) { fprintf(stderr, "Original string had port 4001, but now reporting %d\n", port); goto exit; } + result = multiaddress_get_peer_id(addr2); + if (strcmp(result, orig_address) != 0) { + fprintf(stderr, "New peer id %s does not match %s", result, orig_address); + goto exit; + } + free(result); + result = NULL; + retVal = 1; exit: if (addr != NULL) @@ -174,6 +183,30 @@ int test_multiaddr_peer_id() { return retVal; } +int test_multiaddr_get_peer_id() { + char* orig_address = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG"; + char full_string[255]; + char* result = NULL; + int retVal = 0; + struct MultiAddress *addr = NULL; + + sprintf(full_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s/", orig_address); + + addr = multiaddress_new_from_string(full_string); + + result = multiaddress_get_peer_id(addr); + + if (result == NULL || strcmp(result, orig_address) != 0) + goto exit; + + retVal = 1; + exit: + multiaddress_free(addr); + free(result); + result = NULL; + return retVal; +} + int test_multiaddr_bytes() { int retVal = 0; char* orig_address = "/ip4/127.0.0.1/tcp/4001/"; diff --git a/testing.c b/testing.c index 395f9ea..35e5997 100644 --- a/testing.c +++ b/testing.c @@ -9,6 +9,7 @@ const char* names[] = { "test_int_to_hex", "test_multiaddr_utils", "test_multiaddr_peer_id", + "test_multiaddr_get_peer_id", "test_multiaddr_bytes" }; @@ -19,6 +20,7 @@ int (*funcs[])(void) = { test_int_to_hex, test_multiaddr_utils, test_multiaddr_peer_id, + test_multiaddr_get_peer_id, test_multiaddr_bytes };