Small memory leak fix for protocol comparison

yamux
John Jones 2017-09-07 18:46:03 -05:00
parent 5a9ab47635
commit 09e2a2291f
6 changed files with 44 additions and 6 deletions

View File

@ -41,7 +41,7 @@ int libp2p_datastore_new(struct Datastore** datastore) {
if (*datastore == NULL)
return 0;
(*datastore)->path = NULL;
(*datastore)->datastore_handle = NULL;
(*datastore)->datastore_context = NULL;
(*datastore)->type = NULL;
(*datastore)->storage_max = NULL;
(*datastore)->gc_period = NULL;
@ -67,7 +67,7 @@ int libp2p_datastore_free(struct Datastore* datastore) {
free(datastore->gc_period);
if (datastore->params != NULL)
free(datastore->params);
if (datastore->datastore_handle != NULL)
if (datastore->datastore_context != NULL)
datastore->datastore_close(datastore);
free(datastore);
}

View File

@ -36,7 +36,7 @@ struct Datastore {
int (*datastore_cursor_close)(struct Datastore* datastore);
int (*datastore_cursor_get)(unsigned char** key, int* key_length, unsigned char** value, int* value_length, enum DatastoreCursorOp op, struct Datastore* datastore);
// generic connection and status variables for the datastore
void* datastore_handle; // a handle to the database
void* datastore_context; // a handle to a context that holds connectivity information
};
/***

View File

@ -83,4 +83,13 @@ int os_utils_is_directory(const char* file_name);
*/
unsigned long long os_utils_gmtime();
/**
* String search for platforms without it
* @haystack where to look
* @needle what you're looking for
* @len when to stop looking
* @returns a pointer to where the needle is found in the haystack or NULL if not found
*/
char *strnstr(const char *haystack, const char *needle, size_t len);
#endif /* utils_h */

View File

@ -191,3 +191,29 @@ unsigned long long os_utils_gmtime() {
struct tm *gmt = gmtime(&local);
return (unsigned long long)mktime(gmt);
}
/**
* String search for platforms without it
* @haystack where to look
* @needle what you're looking for
* @len when to stop looking
* @returns a pointer to where the needle is found in the haystack or NULL if not found
*/
char *strnstr(const char *haystack, const char *needle, size_t len)
{
int i;
size_t needle_len;
if (0 == (needle_len = strnlen(needle, len)))
return (char *)haystack;
for (i=0; i<=(int)(len-needle_len); i++)
{
if ((haystack[0] == needle[0]) &&
(0 == strncmp(haystack, needle, needle_len)))
return (char *)haystack;
haystack++;
}
return NULL;
}

View File

@ -3,6 +3,7 @@
#include "libp2p/crypto/encoding/base58.h"
#include "libp2p/net/stream.h"
#include "libp2p/os/utils.h"
#include "libp2p/routing/dht_protocol.h"
#include "libp2p/record/message.h"
#include "libp2p/utils/linked_list.h"
@ -22,7 +23,7 @@ struct DhtContext {
int libp2p_routing_dht_can_handle(const uint8_t* incoming, size_t incoming_size) {
if (incoming_size < 8)
return 0;
char* result = strstr((char*)incoming, "/ipfs/kad");
char* result = strnstr((char*)incoming, "/ipfs/kad", incoming_size);
if (result != NULL && result == (char*)incoming)
return 1;
return 0;

View File

@ -16,6 +16,7 @@
#include "libp2p/secio/exchange.h"
#include "libp2p/net/multistream.h"
#include "libp2p/net/p2pnet.h"
#include "libp2p/os/utils.h"
#include "libp2p/crypto/ephemeral.h"
#include "libp2p/crypto/sha1.h"
#include "libp2p/crypto/sha256.h"
@ -39,10 +40,11 @@ struct SecioContext {
};
int libp2p_secio_can_handle(const uint8_t* incoming, size_t incoming_size) {
const char* protocol = "/secio/1.0.0";
// sanity checks
if (incoming_size < 11)
if (incoming_size < 12)
return 0;
char* result = strstr((char*)incoming, "/secio/1.0.0");
char* result = strnstr((char*)incoming, protocol, incoming_size);
if (result != NULL && result == (char*)incoming)
return 1;
return 0;