Better handling of bad memory allocation

This commit is contained in:
John Jones 2017-10-05 15:14:47 -05:00
parent 996687cfce
commit 71c216defb
25 changed files with 299 additions and 117 deletions

View file

@ -175,9 +175,19 @@ int ipfs_block_free(struct Block* block) {
*/ */
struct Block* ipfs_block_copy(struct Block* original) { struct Block* ipfs_block_copy(struct Block* original) {
struct Block* copy = ipfs_block_new(); struct Block* copy = ipfs_block_new();
copy->data_length = original->data_length; if (copy != NULL) {
copy->data = (unsigned char*) malloc(original->data_length); copy->data_length = original->data_length;
memcpy(copy->data, original->data, original->data_length); copy->data = (unsigned char*) malloc(original->data_length);
copy->cid = ipfs_cid_copy(original->cid); if (copy->data == NULL) {
ipfs_block_free(copy);
return NULL;
}
memcpy(copy->data, original->data, original->data_length);
copy->cid = ipfs_cid_copy(original->cid);
if (copy->cid == NULL) {
ipfs_block_free(copy);
return NULL;
}
}
return copy; return copy;
} }

View file

@ -102,6 +102,8 @@ char* ipfs_blockstore_path_get(const struct FSRepo* fs_repo, const char* filenam
} }
int complete_filename_size = strlen(filepath) + strlen(filename) + 2; int complete_filename_size = strlen(filepath) + strlen(filename) + 2;
char* complete_filename = (char*)malloc(complete_filename_size); char* complete_filename = (char*)malloc(complete_filename_size);
if (complete_filename == NULL)
return NULL;
retVal = os_utils_filepath_join(filepath, filename, complete_filename, complete_filename_size); retVal = os_utils_filepath_join(filepath, filename, complete_filename, complete_filename_size);
return complete_filename; return complete_filename;
} }

View file

@ -147,6 +147,10 @@ struct Cid* ipfs_cid_copy(const struct Cid* original) {
copy->version = original->version; copy->version = original->version;
copy->hash_length = original->hash_length; copy->hash_length = original->hash_length;
copy->hash = (unsigned char*) malloc(original->hash_length); copy->hash = (unsigned char*) malloc(original->hash_length);
if (copy->hash == NULL) {
ipfs_cid_free(copy);
return NULL;
}
memcpy(copy->hash, original->hash, original->hash_length); memcpy(copy->hash, original->hash, original->hash_length);
} }
return copy; return copy;

View file

@ -126,6 +126,10 @@ int ipfs_core_http_process_name(struct IpfsNode* local_node, struct HttpRequest*
struct HttpResponse* res = *response; struct HttpResponse* res = *response;
res->content_type = "application/json"; res->content_type = "application/json";
res->bytes = (uint8_t*) malloc(strlen(local_node->identity->peer->id) + strlen(path) + 30); res->bytes = (uint8_t*) malloc(strlen(local_node->identity->peer->id) + strlen(path) + 30);
if (res->bytes == NULL) {
free(result);
return 0;
}
sprintf((char*)res->bytes, "{ \"Path\": \"%s\" }", result); sprintf((char*)res->bytes, "{ \"Path\": \"%s\" }", result);
res->bytes_size = strlen((char*)res->bytes); res->bytes_size = strlen((char*)res->bytes);
} }
@ -137,6 +141,8 @@ int ipfs_core_http_process_name(struct IpfsNode* local_node, struct HttpRequest*
struct HttpResponse* res = *response; struct HttpResponse* res = *response;
res->content_type = "application/json"; res->content_type = "application/json";
res->bytes = (uint8_t*) malloc(strlen(local_node->identity->peer->id) + strlen(path) + 30); res->bytes = (uint8_t*) malloc(strlen(local_node->identity->peer->id) + strlen(path) + 30);
if (res->bytes == NULL)
return 0;
sprintf((char*)res->bytes, "{ \"Name\": \"%s\"\n \"Value\": \"%s\" }", local_node->identity->peer->id, path); sprintf((char*)res->bytes, "{ \"Name\": \"%s\"\n \"Value\": \"%s\" }", local_node->identity->peer->id, path);
res->bytes_size = strlen((char*)res->bytes); res->bytes_size = strlen((char*)res->bytes);
} }
@ -194,40 +200,44 @@ int ipfs_core_http_process_dht_provide(struct IpfsNode* local_node, struct HttpR
struct HttpResponse* res = *response; struct HttpResponse* res = *response;
res->content_type = "application/json"; res->content_type = "application/json";
res->bytes = (uint8_t*) malloc(1024); res->bytes = (uint8_t*) malloc(1024);
if (!failedCount) { if (res->bytes == NULL) {
// complete success res->bytes_size = 0;
// TODO: do the right thing
snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\"\n" \
"\t\"Type\": \"<int>\"\n"
"\t\"Responses\": [\n"
"\t\t{\n"
"\t\t\t\"ID\": \"<string>\"\n"
"\t\t\t\"Addrs\": [\n"
"\t\t\t\t\"<object>\"\n"
"\t\t\t]\n"
"\t\t}\n"
"\t]\n"
"\t\"Extra\": \"<string>\"\n"
"}\n"
);
} else { } else {
// at least some failed if (!failedCount) {
// TODO: do the right thing // complete success
snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\",\n" \ // TODO: do the right thing
"\t\"Type\": \"<int>\",\n" snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\"\n" \
"\t\"Responses\": [\n" "\t\"Type\": \"<int>\"\n"
"\t\t{\n" "\t\"Responses\": [\n"
"\t\t\t\"ID\": \"<string>\",\n" "\t\t{\n"
"\t\t\t\"Addrs\": [\n" "\t\t\t\"ID\": \"<string>\"\n"
"\t\t\t\t\"<object>\"\n" "\t\t\t\"Addrs\": [\n"
"\t\t\t]\n" "\t\t\t\t\"<object>\"\n"
"\t\t}\n" "\t\t\t]\n"
"\t],\n" "\t\t}\n"
"\t\"Extra\": \"<string>\"\n" "\t]\n"
"}\n" "\t\"Extra\": \"<string>\"\n"
); "}\n"
);
} else {
// at least some failed
// TODO: do the right thing
snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\",\n" \
"\t\"Type\": \"<int>\",\n"
"\t\"Responses\": [\n"
"\t\t{\n"
"\t\t\t\"ID\": \"<string>\",\n"
"\t\t\t\"Addrs\": [\n"
"\t\t\t\t\"<object>\"\n"
"\t\t\t]\n"
"\t\t}\n"
"\t],\n"
"\t\"Extra\": \"<string>\"\n"
"}\n"
);
}
res->bytes_size = strlen((char*)res->bytes);
} }
res->bytes_size = strlen((char*)res->bytes);
return failedCount < request->arguments->total; return failedCount < request->arguments->total;
} }
@ -322,7 +332,9 @@ char* ipfs_core_http_request_build_url_start(struct IpfsNode* local_node) {
sprintf(port, "%d", portInt); sprintf(port, "%d", portInt);
int len = 18 + strlen(host) + strlen(port); int len = 18 + strlen(host) + strlen(port);
char* retVal = malloc(len); char* retVal = malloc(len);
sprintf(retVal, "http://%s:%s/api/v0", host, port); if (retVal != NULL) {
sprintf(retVal, "http://%s:%s/api/v0", host, port);
}
free(host); free(host);
multiaddress_free(ma); multiaddress_free(ma);
return retVal; return retVal;
@ -338,18 +350,20 @@ int ipfs_core_http_request_add_commands(struct HttpRequest* request, char** url)
// command // command
int addl_length = strlen(request->command) + 2; int addl_length = strlen(request->command) + 2;
char* string1 = (char*) malloc(strlen(*url) + addl_length); char* string1 = (char*) malloc(strlen(*url) + addl_length);
sprintf(string1, "%s/%s", *url, request->command); if (string1 != NULL) {
free(*url); sprintf(string1, "%s/%s", *url, request->command);
*url = string1;
// sub_command
if (request->sub_command != NULL) {
addl_length = strlen(request->sub_command) + 2;
string1 = (char*) malloc(strlen(*url) + addl_length);
sprintf(string1, "%s/%s", *url, request->sub_command);
free(*url); free(*url);
*url = string1; *url = string1;
// sub_command
if (request->sub_command != NULL) {
addl_length = strlen(request->sub_command) + 2;
string1 = (char*) malloc(strlen(*url) + addl_length);
sprintf(string1, "%s/%s", *url, request->sub_command);
free(*url);
*url = string1;
}
} }
return 1; return string1 != NULL;
} }
/*** /***

View file

@ -174,6 +174,11 @@ void* ipfs_null_listen (void *ptr)
connection_param->local_node = listen_param->local_node; connection_param->local_node = listen_param->local_node;
connection_param->port = listen_param->port; connection_param->port = listen_param->port;
connection_param->ip = malloc(INET_ADDRSTRLEN); connection_param->ip = malloc(INET_ADDRSTRLEN);
if (connection_param->ip == NULL) {
// we are out of memory
free(connection_param);
continue;
}
if (inet_ntop(AF_INET, &(listen_param->ipv4), connection_param->ip, INET_ADDRSTRLEN) == NULL) { if (inet_ntop(AF_INET, &(listen_param->ipv4), connection_param->ip, INET_ADDRSTRLEN) == NULL) {
free(connection_param->ip); free(connection_param->ip);
connection_param->ip = NULL; connection_param->ip = NULL;

View file

@ -66,6 +66,10 @@ int ipfs_ping (int argc, char **argv)
// perhaps they passed an IP and port // perhaps they passed an IP and port
if (argc >= 3) { if (argc >= 3) {
char* str = malloc(strlen(argv[2]) + strlen(argv[3]) + 100); char* str = malloc(strlen(argv[2]) + strlen(argv[3]) + 100);
if (str == NULL) {
// memory issue
goto exit;
}
sprintf(str, "/ip4/%s/tcp/%s", argv[2], argv[3]); sprintf(str, "/ip4/%s/tcp/%s", argv[2], argv[3]);
peer_to_ping = libp2p_peer_new(); peer_to_ping = libp2p_peer_new();
if (peer_to_ping) { if (peer_to_ping) {

View file

@ -519,6 +519,10 @@ int ipfs_bitswap_message_protobuf_encode(const struct BitswapMessage* message, u
// protobuf it // protobuf it
size_t temp_size = ipfs_blocks_block_protobuf_encode_size(entry); size_t temp_size = ipfs_blocks_block_protobuf_encode_size(entry);
uint8_t* temp = (uint8_t*) malloc(temp_size); uint8_t* temp = (uint8_t*) malloc(temp_size);
if (temp == NULL) {
// memory issues
return 0;
}
if (!ipfs_blocks_block_protobuf_encode(entry, temp, temp_size, &temp_size)) { if (!ipfs_blocks_block_protobuf_encode(entry, temp, temp_size, &temp_size)) {
free(temp); free(temp);
return 0; return 0;
@ -536,6 +540,9 @@ int ipfs_bitswap_message_protobuf_encode(const struct BitswapMessage* message, u
if (message->wantlist != NULL) { if (message->wantlist != NULL) {
size_t temp_size = ipfs_bitswap_wantlist_protobuf_encode_size(message->wantlist); size_t temp_size = ipfs_bitswap_wantlist_protobuf_encode_size(message->wantlist);
uint8_t* temp = (uint8_t*) malloc(temp_size); uint8_t* temp = (uint8_t*) malloc(temp_size);
if (temp == NULL) {
return 0;
}
if (!ipfs_bitswap_wantlist_protobuf_encode(message->wantlist, temp, temp_size, &temp_size)) { if (!ipfs_bitswap_wantlist_protobuf_encode(message->wantlist, temp, temp_size, &temp_size)) {
free(temp); free(temp);
return 0; return 0;
@ -665,6 +672,9 @@ int ipfs_bitswap_message_add_wantlist_items(struct BitswapMessage* message, stru
struct WantlistEntry* entry = ipfs_bitswap_wantlist_entry_new(); struct WantlistEntry* entry = ipfs_bitswap_wantlist_entry_new();
entry->block_size = ipfs_cid_protobuf_encode_size(cidEntry->cid); entry->block_size = ipfs_cid_protobuf_encode_size(cidEntry->cid);
entry->block = (unsigned char*) malloc(entry->block_size); entry->block = (unsigned char*) malloc(entry->block_size);
if (entry->block == NULL) {
return 0;
}
if (!ipfs_cid_protobuf_encode(cidEntry->cid, entry->block, entry->block_size, &entry->block_size)) { if (!ipfs_cid_protobuf_encode(cidEntry->cid, entry->block, entry->block_size, &entry->block_size)) {
// TODO: we should do more than return a half-baked list // TODO: we should do more than return a half-baked list
return 0; return 0;

View file

@ -36,16 +36,20 @@ int ipfs_exporter_get_node(struct IpfsNode* local_node, const unsigned char* has
goto exit; goto exit;
} }
libp2p_logger_debug("exporter", "get_node got a value. Converting it to a HashtableNode\n");
// unprotobuf // unprotobuf
if (!ipfs_hashtable_node_protobuf_decode(buffer, buffer_size, result)) { if (!ipfs_hashtable_node_protobuf_decode(buffer, buffer_size, result)) {
libp2p_logger_debug("exporter", "Conversion to HashtableNode not successful\n"); libp2p_logger_error("exporter", "Conversion to HashtableNode not successful\n");
goto exit; goto exit;
} }
// copy in the hash // copy in the hash
(*result)->hash_size = hash_size; (*result)->hash_size = hash_size;
(*result)->hash = malloc(hash_size); (*result)->hash = malloc(hash_size);
if ( (*result)->hash == NULL) {
// memory issue
libp2p_logger_error("exporter", "get_node: Unable to allocate memory.\n");
goto exit;
}
memcpy((*result)->hash, hash, hash_size); memcpy((*result)->hash, hash, hash_size);
retVal = 1; retVal = 1;

View file

@ -220,6 +220,12 @@ int ipfs_import_file(const char* root_dir, const char* fileName, struct Hashtabl
} else { } else {
free(path); free(path);
path = malloc(strlen(root_dir) + strlen(file) + 2); path = malloc(strlen(root_dir) + strlen(file) + 2);
if (path == NULL) {
// memory issue
if (file != NULL)
free(file);
return 0;
}
os_utils_filepath_join(root_dir, file, path, strlen(root_dir) + strlen(file) + 2); os_utils_filepath_join(root_dir, file, path, strlen(root_dir) + strlen(file) + 2);
new_root_dir = path; new_root_dir = path;
} }
@ -328,6 +334,9 @@ struct FileList* ipfs_import_get_filelist(struct CliArguments* args) {
continue; continue;
} }
struct FileList* current = (struct FileList*)malloc(sizeof(struct FileList)); struct FileList* current = (struct FileList*)malloc(sizeof(struct FileList));
if (current == NULL) {
return NULL;
}
current->next = NULL; current->next = NULL;
current->file_name = args->argv[i]; current->file_name = args->argv[i];
// now wire it in // now wire it in

View file

@ -28,9 +28,16 @@ int ipfs_resolver_next_path(const char* path, char** next_part) {
char* pos = strchr(&path[i+1], '/'); char* pos = strchr(&path[i+1], '/');
if (pos == NULL) { if (pos == NULL) {
*next_part = (char*)malloc(strlen(path) + 1); *next_part = (char*)malloc(strlen(path) + 1);
if ( *next_part == NULL) {
// memory issue
return 0;
}
strcpy(*next_part, path); strcpy(*next_part, path);
} else { } else {
*next_part = (char*)malloc(pos - &path[i] + 1); *next_part = (char*)malloc(pos - &path[i] + 1);
if (*next_part == NULL) {
return 0;
}
strncpy(*next_part, &path[i], pos-&path[i]); strncpy(*next_part, &path[i], pos-&path[i]);
(*next_part)[pos-&path[i]] = 0; (*next_part)[pos-&path[i]] = 0;
} }
@ -145,9 +152,11 @@ struct HashtableNode* ipfs_resolver_remote_get(const char* path, struct Hashtabl
message->key_size = strlen(key); message->key_size = strlen(key);
size_t b58size = 100; size_t b58size = 100;
uint8_t *b58key = (uint8_t *) malloc(b58size); uint8_t *b58key = (uint8_t *) malloc(b58size);
libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size); if (b58key == NULL) {
libp2p_logger_debug("resolver", "Attempting to use kademlia to get key %s.\n", b58key); libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size);
free(b58key); libp2p_logger_debug("resolver", "Attempting to use kademlia to get key %s.\n", b58key);
free(b58key);
}
size_t message_protobuf_size = libp2p_message_protobuf_encode_size(message); size_t message_protobuf_size = libp2p_message_protobuf_encode_size(message);
unsigned char message_protobuf[message_protobuf_size]; unsigned char message_protobuf[message_protobuf_size];
libp2p_message_protobuf_encode(message, message_protobuf, message_protobuf_size, &message_protobuf_size); libp2p_message_protobuf_encode(message, message_protobuf, message_protobuf_size, &message_protobuf_size);

View file

@ -41,6 +41,9 @@ void stripit(int argc, char** argv) {
char* old_arg = argv[argc]; char* old_arg = argv[argc];
int full_length = strlen(old_arg); int full_length = strlen(old_arg);
char *tmp = (char*) malloc(full_length + 1); char *tmp = (char*) malloc(full_length + 1);
if (tmp == NULL) {
return;
}
char* ptr1 = &old_arg[1]; char* ptr1 = &old_arg[1];
strcpy(tmp, ptr1); strcpy(tmp, ptr1);
tmp[strlen(tmp)-1] = 0; tmp[strlen(tmp)-1] = 0;

View file

@ -29,6 +29,7 @@ enum WireType ipfs_node_link_message_fields[] = { WIRETYPE_LENGTH_DELIMITED, WIR
* @Param name: The name of the link (char *) * @Param name: The name of the link (char *)
* @Param size: Size of the link (size_t) * @Param size: Size of the link (size_t)
* @Param ahash: An Qmhash * @Param ahash: An Qmhash
* @returns true(1) on success, false(0) otherwise
*/ */
int ipfs_node_link_create(char * name, unsigned char * ahash, size_t hash_size, struct NodeLink** node_link) int ipfs_node_link_create(char * name, unsigned char * ahash, size_t hash_size, struct NodeLink** node_link)
{ {
@ -40,12 +41,18 @@ int ipfs_node_link_create(char * name, unsigned char * ahash, size_t hash_size,
// hash // hash
link->hash_size = hash_size; link->hash_size = hash_size;
link->hash = (unsigned char*)malloc(hash_size); link->hash = (unsigned char*)malloc(hash_size);
if (link->hash == NULL) {
ipfs_node_link_free(link);
*node_link = NULL;
return 0;
}
memcpy(link->hash, ahash, hash_size); memcpy(link->hash, ahash, hash_size);
// name // name
if (name != NULL && strlen(name) > 0) { if (name != NULL && strlen(name) > 0) {
link->name = malloc(strlen(name) + 1); link->name = malloc(strlen(name) + 1);
if ( link->name == NULL) { if ( link->name == NULL) {
free(link); ipfs_node_link_free(link);
*node_link = NULL;
return 0; return 0;
} }
strcpy(link->name, name); strcpy(link->name, name);
@ -192,6 +199,9 @@ int ipfs_node_link_protobuf_decode(unsigned char* buffer, size_t buffer_length,
} }
link->hash_size = hash_size - 2; link->hash_size = hash_size - 2;
link->hash = (unsigned char*)malloc(link->hash_size); link->hash = (unsigned char*)malloc(link->hash_size);
if (link->hash == NULL) {
goto exit;
}
memcpy((char*)link->hash, (char*)&hash[2], link->hash_size); memcpy((char*)link->hash, (char*)&hash[2], link->hash_size);
free(hash); free(hash);
pos += bytes_read; pos += bytes_read;
@ -729,13 +739,18 @@ int Node_Resolve(char ** result, char * input1)
char * tr; char * tr;
char * end; char * end;
tr=strtok_r(input,"/",&end); tr=strtok_r(input,"/",&end);
int retVal = 1;
for(int i = 0;tr;i++) for(int i = 0;tr;i++)
{ {
result[i] = (char *) malloc(strlen(tr)+1); result[i] = (char *) malloc(strlen(tr)+1);
strcpy(result[i], tr); if (result[i] != NULL) {
strcpy(result[i], tr);
} else {
retVal = 0;
}
tr=strtok_r(NULL,"/",&end); tr=strtok_r(NULL,"/",&end);
} }
return 1; return retVal;
} }
/*Node_Resolve_Links /*Node_Resolve_Links
@ -751,6 +766,9 @@ struct Link_Proc * Node_Resolve_Links(struct HashtableNode * N, char * path)
} }
int expected_link_ammount = Node_Resolve_Max_Size(path); int expected_link_ammount = Node_Resolve_Max_Size(path);
struct Link_Proc * LProc = (struct Link_Proc *) malloc(sizeof(struct Link_Proc) + sizeof(struct NodeLink) * expected_link_ammount); struct Link_Proc * LProc = (struct Link_Proc *) malloc(sizeof(struct Link_Proc) + sizeof(struct NodeLink) * expected_link_ammount);
if (LProc == NULL) {
return NULL;
}
LProc->ammount = 0; LProc->ammount = 0;
char * linknames[expected_link_ammount]; char * linknames[expected_link_ammount];
Node_Resolve(linknames, path); Node_Resolve(linknames, path);
@ -761,8 +779,10 @@ struct Link_Proc * Node_Resolve_Links(struct HashtableNode * N, char * path)
if(proclink) if(proclink)
{ {
LProc->links[i] = (struct NodeLink *)malloc(sizeof(struct NodeLink)); LProc->links[i] = (struct NodeLink *)malloc(sizeof(struct NodeLink));
memcpy(LProc->links[i], proclink, sizeof(struct NodeLink)); if (LProc->links[i] == NULL) { // TODO: What should we do if memory wasn't allocated here?
LProc->ammount++; memcpy(LProc->links[i], proclink, sizeof(struct NodeLink));
LProc->ammount++;
}
free(proclink); free(proclink);
} }
} }

View file

@ -41,6 +41,10 @@ int ipfs_namesys_resolver_resolve_once(struct IpfsNode* local_node, const char*
if (local_node->repo->config->datastore->datastore_get(cid->hash, cid->hash_length, &record, local_node->repo->config->datastore)) { if (local_node->repo->config->datastore->datastore_get(cid->hash, cid->hash_length, &record, local_node->repo->config->datastore)) {
// we are able to handle this locally... return the results // we are able to handle this locally... return the results
*results = (char*) malloc(record->value_size + 1); *results = (char*) malloc(record->value_size + 1);
if (*results == NULL) {
ipfs_cid_free(cid);
return 0;
}
memset(*results, 0, record->value_size + 1); memset(*results, 0, record->value_size + 1);
memcpy(*results, record->value, record->value_size); memcpy(*results, record->value, record->value_size);
ipfs_cid_free(cid); ipfs_cid_free(cid);
@ -64,6 +68,9 @@ int ipfs_namesys_resolver_resolve_once(struct IpfsNode* local_node, const char*
int ipfs_namesys_resolver_resolve(struct IpfsNode* local_node, const char* path, int recursive, char** results) { int ipfs_namesys_resolver_resolve(struct IpfsNode* local_node, const char* path, int recursive, char** results) {
char* result = NULL; char* result = NULL;
char* current_path = (char*) malloc(strlen(path) + 1); char* current_path = (char*) malloc(strlen(path) + 1);
if (current_path == NULL) {
return 0;
}
strcpy(current_path, path); strcpy(current_path, path);
// if we go more than 10 deep, bail // if we go more than 10 deep, bail
@ -84,7 +91,8 @@ int ipfs_namesys_resolver_resolve(struct IpfsNode* local_node, const char* path,
// result will not be NULL // result will not be NULL
free(current_path); free(current_path);
current_path = (char*) malloc(strlen(result)+1); current_path = (char*) malloc(strlen(result)+1);
strcpy(current_path, result); if (current_path != NULL)
strcpy(current_path, result);
free(result); free(result);
counter++; counter++;
} while(recursive && is_ipns_string(current_path)); } while(recursive && is_ipns_string(current_path));

View file

@ -131,6 +131,8 @@ int ipfs_namesys_hex_string_to_bytes(const unsigned char* hex, unsigned char** b
// allocate memory // allocate memory
*buffer = (unsigned char*)malloc( hex_size / 2 ); *buffer = (unsigned char*)malloc( hex_size / 2 );
unsigned char* ptr = *buffer; unsigned char* ptr = *buffer;
if (ptr == NULL)
return ErrAllocFailed;
// convert string // convert string
for(size_t i = 0; i < hex_size; i++) { for(size_t i = 0; i < hex_size; i++) {

View file

@ -7,8 +7,10 @@
char* alloc_and_copy(char* source) { char* alloc_and_copy(char* source) {
unsigned long strLen = strlen(source); unsigned long strLen = strlen(source);
char* result = malloc(sizeof(char) * (strLen + 1)); char* result = malloc(sizeof(char) * (strLen + 1));
strncpy(result, source, strLen); if (result != NULL) {
result[strLen] = 0; strncpy(result, source, strLen);
result[strLen] = 0;
}
return result; return result;
} }

View file

@ -140,24 +140,34 @@ int ipfs_repo_config_init(struct RepoConfig* config, unsigned int num_bits_for_k
// swarm addresses // swarm addresses
char* addr1 = malloc(64); char* addr1 = malloc(64);
sprintf(addr1, "/ip4/0.0.0.0/tcp/%d", swarm_port); if (addr1 != NULL) {
config->addresses->swarm_head = libp2p_utils_linked_list_new(); sprintf(addr1, "/ip4/0.0.0.0/tcp/%d", swarm_port);
config->addresses->swarm_head->item = malloc(strlen(addr1) + 1); config->addresses->swarm_head = libp2p_utils_linked_list_new();
strcpy(config->addresses->swarm_head->item, addr1); if (config->addresses->swarm_head != NULL) {
config->addresses->swarm_head->item = malloc(strlen(addr1) + 1);
if (config->addresses->swarm_head->item != NULL) {
strcpy(config->addresses->swarm_head->item, addr1);
}
sprintf(addr1, "/ip6/::/tcp/%d", swarm_port); sprintf(addr1, "/ip6/::/tcp/%d", swarm_port);
config->addresses->swarm_head->next = libp2p_utils_linked_list_new(); config->addresses->swarm_head->next = libp2p_utils_linked_list_new();
config->addresses->swarm_head->next->item = malloc(strlen(addr1) + 1); if (config->addresses->swarm_head->next != NULL) {
strcpy(config->addresses->swarm_head->next->item, addr1); config->addresses->swarm_head->next->item = malloc(strlen(addr1) + 1);
if (config->addresses->swarm_head->next->item != NULL)
int port_adder = swarm_port - 4001; strcpy(config->addresses->swarm_head->next->item, addr1);
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 5001 + port_adder); }
config->addresses->api = malloc(strlen(addr1)+1); }
strcpy(config->addresses->api, addr1); int port_adder = swarm_port - 4001;
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 8080 + port_adder); sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 5001 + port_adder);
config->addresses->gateway = malloc(strlen(addr1)+1); config->addresses->api = malloc(strlen(addr1)+1);
strcpy(config->addresses->gateway, addr1); if (config->addresses->api != NULL)
free(addr1); strcpy(config->addresses->api, addr1);
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 8080 + port_adder);
config->addresses->gateway = malloc(strlen(addr1)+1);
if (config->addresses->gateway != NULL)
strcpy(config->addresses->gateway, addr1);
free(addr1);
}
config->discovery.mdns.enabled = 1; config->discovery.mdns.enabled = 1;
config->discovery.mdns.interval = 10; config->discovery.mdns.interval = 10;

View file

@ -6,8 +6,10 @@
char* alloc_and_fill(char* source) { char* alloc_and_fill(char* source) {
char* newString = malloc(sizeof(char) * (strlen(source) + 1)); char* newString = malloc(sizeof(char) * (strlen(source) + 1));
strncpy(newString, source, strlen(source)); if (newString != NULL) {
newString[strlen(source)] = 0; strncpy(newString, source, strlen(source));
newString[strlen(source)] = 0;
}
return newString; return newString;
} }

View file

@ -150,7 +150,8 @@ int ipfs_repo_fsrepo_new(const char* repo_path, struct RepoConfig* config, struc
} else { } else {
int len = strlen(repo_path) + 1; int len = strlen(repo_path) + 1;
(*repo)->path = (char*)malloc(len); (*repo)->path = (char*)malloc(len);
strncpy((*repo)->path, repo_path, len); if ( (*repo)->path != NULL)
strncpy((*repo)->path, repo_path, len);
} }
// allocate other structures // allocate other structures
if (config != NULL) if (config != NULL)
@ -574,10 +575,13 @@ int ipfs_repo_fsrepo_node_get(const unsigned char* hash, size_t hash_length, voi
if (retVal == 1) { if (retVal == 1) {
*node_size = ipfs_hashtable_node_protobuf_encode_size(node); *node_size = ipfs_hashtable_node_protobuf_encode_size(node);
*node_obj = malloc(*node_size); *node_obj = malloc(*node_size);
if (*node_obj == NULL) {
ipfs_hashtable_node_free(node);
return 0;
}
retVal = ipfs_hashtable_node_protobuf_encode(node, *node_obj, *node_size, node_size); retVal = ipfs_hashtable_node_protobuf_encode(node, *node_obj, *node_size, node_size);
} }
if (node != NULL) ipfs_hashtable_node_free(node);
ipfs_hashtable_node_free(node);
return retVal; return retVal;
} }

View file

@ -220,6 +220,10 @@ int repo_fsrepo_lmdb_put(struct DatastoreRecord* datastore_record, const struct
journalstore_record = lmdb_journal_record_new(); journalstore_record = lmdb_journal_record_new();
journalstore_record->hash_size = datastore_record->key_size; journalstore_record->hash_size = datastore_record->key_size;
journalstore_record->hash = malloc(datastore_record->key_size); journalstore_record->hash = malloc(datastore_record->key_size);
if (journalstore_record->hash == NULL) {
libp2p_logger_error("lmdb_datastore", "put: Unable to allocate memory for key.\n");
return 0;
}
memcpy(journalstore_record->hash, datastore_record->key, datastore_record->key_size); memcpy(journalstore_record->hash, datastore_record->key, datastore_record->key_size);
journalstore_record->timestamp = datastore_record->timestamp; journalstore_record->timestamp = datastore_record->timestamp;
// look up the corresponding journalstore record for possible updating // look up the corresponding journalstore record for possible updating
@ -264,17 +268,23 @@ int repo_fsrepo_lmdb_put(struct DatastoreRecord* datastore_record, const struct
// add it to the journalstore // add it to the journalstore
journalstore_record = lmdb_journal_record_new(); journalstore_record = lmdb_journal_record_new();
journalstore_record->hash = (uint8_t*) malloc(datastore_record->key_size); journalstore_record->hash = (uint8_t*) malloc(datastore_record->key_size);
memcpy(journalstore_record->hash, datastore_record->key, datastore_record->key_size); if (journalstore_record->hash == NULL) {
journalstore_record->hash_size = datastore_record->key_size; libp2p_logger_error("lmdb_datastore", "Unable to allocate memory to add record to journalstore.\n");
journalstore_record->timestamp = datastore_record->timestamp; lmdb_journalstore_cursor_close(journalstore_cursor, 0);
journalstore_record->pending = 1; // TODO: Calculate this correctly lmdb_journal_record_free(journalstore_record);
journalstore_record->pin = 1; } else {
if (!lmdb_journalstore_journal_add(journalstore_cursor, journalstore_record)) { memcpy(journalstore_record->hash, datastore_record->key, datastore_record->key_size);
libp2p_logger_error("lmdb_datastore", "Datastore record was added, but problem adding Journalstore record. Continuing.\n"); journalstore_record->hash_size = datastore_record->key_size;
journalstore_record->timestamp = datastore_record->timestamp;
journalstore_record->pending = 1; // TODO: Calculate this correctly
journalstore_record->pin = 1;
if (!lmdb_journalstore_journal_add(journalstore_cursor, journalstore_record)) {
libp2p_logger_error("lmdb_datastore", "Datastore record was added, but problem adding Journalstore record. Continuing.\n");
}
lmdb_journalstore_cursor_close(journalstore_cursor, 0);
lmdb_journal_record_free(journalstore_record);
retVal = 1;
} }
lmdb_journalstore_cursor_close(journalstore_cursor, 0);
lmdb_journal_record_free(journalstore_record);
retVal = 1;
} }
} else { } else {
// datastore record was unable to be added. // datastore record was unable to be added.
@ -321,10 +331,25 @@ int repo_fsrepro_lmdb_open(int argc, char** argv, struct Datastore* datastore) {
} }
struct lmdb_context *db_context = (struct lmdb_context *) malloc(sizeof(struct lmdb_context)); struct lmdb_context *db_context = (struct lmdb_context *) malloc(sizeof(struct lmdb_context));
if (db_context == NULL) {
mdb_env_close(mdb_env);
return 0;
}
datastore->datastore_context = (void*) db_context; datastore->datastore_context = (void*) db_context;
db_context->db_environment = (void*)mdb_env; db_context->db_environment = (void*)mdb_env;
db_context->datastore_db = (MDB_dbi*) malloc(sizeof(MDB_dbi)); db_context->datastore_db = (MDB_dbi*) malloc(sizeof(MDB_dbi));
if (db_context->datastore_db == NULL) {
mdb_env_close(mdb_env);
free(db_context);
return 0;
}
db_context->journal_db = (MDB_dbi*) malloc(sizeof(MDB_dbi)); db_context->journal_db = (MDB_dbi*) malloc(sizeof(MDB_dbi));
if (db_context->journal_db == NULL) {
free(db_context->datastore_db);
free(db_context);
mdb_env_close(mdb_env);
return 0;
}
// open the 2 databases // open the 2 databases
if (mdb_txn_begin(mdb_env, NULL, 0, &db_context->current_transaction) != 0) { if (mdb_txn_begin(mdb_env, NULL, 0, &db_context->current_transaction) != 0) {

View file

@ -101,8 +101,12 @@ int lmdb_journalstore_build_record(const struct MDB_val* db_key, const struct MD
} }
rec->hash_size = db_value->mv_size - 2; rec->hash_size = db_value->mv_size - 2;
rec->hash = malloc(rec->hash_size); rec->hash = malloc(rec->hash_size);
uint8_t *val = (uint8_t*)db_value->mv_data; if (rec->hash != NULL) {
memcpy(rec->hash, &val[2], rec->hash_size); uint8_t *val = (uint8_t*)db_value->mv_data;
memcpy(rec->hash, &val[2], rec->hash_size);
} else {
return 0;
}
return 1; return 1;
} }

View file

@ -95,7 +95,8 @@ int make_ipfs_repository(const char* path, int swarm_port, struct Libp2pVector*
printf("peer identity: %s\n", fs_repo->config->identity->peer->id); printf("peer identity: %s\n", fs_repo->config->identity->peer->id);
if (peer_id != NULL) { if (peer_id != NULL) {
*peer_id = malloc(fs_repo->config->identity->peer->id_size + 1); *peer_id = malloc(fs_repo->config->identity->peer->id_size + 1);
strcpy(*peer_id, fs_repo->config->identity->peer->id); if (*peer_id != NULL)
strcpy(*peer_id, fs_repo->config->identity->peer->id);
} }
// make sure the repository exists // make sure the repository exists

View file

@ -50,6 +50,7 @@ int ipfs_routing_kademlia_get_value(struct IpfsRouting* routing, const unsigned
* @returns true(1) on success, otherwise false(0) * @returns true(1) on success, otherwise false(0)
*/ */
int ipfs_routing_kademlia_find_providers(struct IpfsRouting* routing, const unsigned char* key, size_t key_size, struct Libp2pVector** results) { int ipfs_routing_kademlia_find_providers(struct IpfsRouting* routing, const unsigned char* key, size_t key_size, struct Libp2pVector** results) {
int retVal = 1;
*results = libp2p_utils_vector_new(1); *results = libp2p_utils_vector_new(1);
struct Libp2pVector* vector = *results; struct Libp2pVector* vector = *results;
// see if I can provide it // see if I can provide it
@ -70,26 +71,30 @@ int ipfs_routing_kademlia_find_providers(struct IpfsRouting* routing, const unsi
if (vector->total == 0) { if (vector->total == 0) {
// search requires null terminated key // search requires null terminated key
char* key_nt = malloc(key_size + 1); char* key_nt = malloc(key_size + 1);
strncpy(key_nt, (char*)key, key_size); if (key_nt != NULL) {
key_nt[key_size] = 0; strncpy(key_nt, (char*)key, key_size);
struct MultiAddress** list = search_kademlia(key_nt, 3); key_nt[key_size] = 0;
free(key_nt); struct MultiAddress** list = search_kademlia(key_nt, 3);
if (list != NULL) { free(key_nt);
int i = 0; if (list != NULL) {
while (list[i] != NULL) { int i = 0;
struct MultiAddress* current = list[i]; while (list[i] != NULL) {
libp2p_utils_vector_add(vector, current); struct MultiAddress* current = list[i];
i++; libp2p_utils_vector_add(vector, current);
i++;
}
} }
} else {
retVal = 0;
} }
} }
if (vector->total == 0) { if (vector->total == 0) {
// we were unable to find it, even on the network // we were unable to find it, even on the network
libp2p_utils_vector_free(vector); libp2p_utils_vector_free(vector);
vector = NULL; vector = NULL;
return 0; retVal = 0;
} }
return 1; return retVal;
} }
/** /**

View file

@ -97,6 +97,8 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *
// protobuf the node // protobuf the node
int protobuf_size = ipfs_hashtable_node_protobuf_encode_size(node); int protobuf_size = ipfs_hashtable_node_protobuf_encode_size(node);
*val = malloc(protobuf_size); *val = malloc(protobuf_size);
if (*val == NULL)
goto exit;
if (ipfs_hashtable_node_protobuf_encode(node, *val, protobuf_size, vlen) == 0) { if (ipfs_hashtable_node_protobuf_encode(node, *val, protobuf_size, vlen) == 0) {
goto exit; goto exit;

View file

@ -53,12 +53,20 @@ int ipfs_routing_online_find_remote_providers(struct IpfsRouting* routing, const
message->message_type = MESSAGE_TYPE_GET_PROVIDERS; message->message_type = MESSAGE_TYPE_GET_PROVIDERS;
message->key_size = key_size; message->key_size = key_size;
message->key = malloc(message->key_size); message->key = malloc(message->key_size);
if (message->key == NULL) {
libp2p_message_free(message);
return 0;
}
memcpy(message->key, key, message->key_size); memcpy(message->key, key, message->key_size);
size_t b58size = 100; if (libp2p_logger_watching_class("online")) {
uint8_t *b58key = (uint8_t *) malloc(b58size); size_t b58size = 100;
libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size); uint8_t *b58key = (uint8_t *) malloc(b58size);
libp2p_logger_debug("online", "find_remote_providers looking for key %s.\n", b58key); if (b58key != NULL) {
free(b58key); libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size);
libp2p_logger_debug("online", "find_remote_providers looking for key %s.\n", b58key);
free(b58key);
}
}
// loop through the connected peers, asking for the hash // loop through the connected peers, asking for the hash
struct Libp2pLinkedList* current_entry = routing->local_node->peerstore->head_entry; struct Libp2pLinkedList* current_entry = routing->local_node->peerstore->head_entry;
while (current_entry != NULL) { while (current_entry != NULL) {
@ -246,6 +254,10 @@ int ipfs_routing_online_provide(struct IpfsRouting* routing, const unsigned char
struct KademliaMessage* msg = libp2p_message_new(); struct KademliaMessage* msg = libp2p_message_new();
msg->key_size = key_size; msg->key_size = key_size;
msg->key = malloc(msg->key_size); msg->key = malloc(msg->key_size);
if (msg->key == NULL) {
libp2p_message_free(msg);
return 0;
}
memcpy(msg->key, key, msg->key_size); memcpy(msg->key, key, msg->key_size);
msg->message_type = MESSAGE_TYPE_ADD_PROVIDER; msg->message_type = MESSAGE_TYPE_ADD_PROVIDER;
msg->provider_peer_head = libp2p_utils_linked_list_new(); msg->provider_peer_head = libp2p_utils_linked_list_new();
@ -333,6 +345,10 @@ int ipfs_routing_online_get_peer_value(ipfs_routing* routing, const struct Libp2
struct KademliaMessage* msg = libp2p_message_new(); struct KademliaMessage* msg = libp2p_message_new();
msg->key_size = key_size; msg->key_size = key_size;
msg->key = malloc(msg->key_size); msg->key = malloc(msg->key_size);
if (msg->key == NULL) {
libp2p_message_free(msg);
return 0;
}
memcpy(msg->key, key, msg->key_size); memcpy(msg->key, key, msg->key_size);
msg->message_type = MESSAGE_TYPE_GET_VALUE; msg->message_type = MESSAGE_TYPE_GET_VALUE;

View file

@ -139,12 +139,14 @@ int ipfs_unixfs_add_data(unsigned char* data, size_t data_length, struct UnixFS*
} }
// debug: display hash // debug: display hash
size_t b58size = 100; if (libp2p_logger_watching_class("unixfs")) {
uint8_t *b58key = (uint8_t *) malloc(b58size); size_t b58size = 100;
if (b58key != NULL) { uint8_t *b58key = (uint8_t *) malloc(b58size);
libp2p_crypto_encoding_base58_encode(unix_fs->hash, unix_fs->hash_length, &b58key, &b58size); if (b58key != NULL) {
libp2p_logger_debug("unixfs", "Saving hash of %s to unixfs object.\n", b58key); libp2p_crypto_encoding_base58_encode(unix_fs->hash, unix_fs->hash_length, &b58key, &b58size);
free(b58key); libp2p_logger_debug("unixfs", "Saving hash of %s to unixfs object.\n", b58key);
free(b58key);
}
} }
return 1; return 1;
@ -157,6 +159,9 @@ int ipfs_unixfs_add_blocksize(const struct UnixFSBlockSizeNode* blocksize, struc
if (last == NULL) { if (last == NULL) {
// we're the first one // we're the first one
unix_fs->block_size_head = (struct UnixFSBlockSizeNode*)malloc(sizeof(struct UnixFSBlockSizeNode)); unix_fs->block_size_head = (struct UnixFSBlockSizeNode*)malloc(sizeof(struct UnixFSBlockSizeNode));
if (unix_fs->block_size_head == NULL) {
return 0;
}
unix_fs->block_size_head->block_size = blocksize->block_size; unix_fs->block_size_head->block_size = blocksize->block_size;
unix_fs->block_size_head->next = NULL; unix_fs->block_size_head->next = NULL;
} else { } else {
@ -165,6 +170,8 @@ int ipfs_unixfs_add_blocksize(const struct UnixFSBlockSizeNode* blocksize, struc
last = last->next; last = last->next;
} }
last->next = (struct UnixFSBlockSizeNode*)malloc(sizeof(struct UnixFSBlockSizeNode)); last->next = (struct UnixFSBlockSizeNode*)malloc(sizeof(struct UnixFSBlockSizeNode));
if (last->next == NULL)
return 0;
last->next->block_size = blocksize->block_size; last->next->block_size = blocksize->block_size;
last->next->next = NULL; last->next->next = NULL;
} }