From 2cc7f52fbf659273c5705dcbdf4dbc237bbee5a3 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Tue, 26 Sep 2017 09:43:10 -0500 Subject: [PATCH] fixed attempt to free a non allocated string --- importer/importer.c | 32 ++++++++++++-------------------- include/ipfs/importer/importer.h | 3 ++- main/main.c | 2 +- test/core/test_api.h | 3 ++- test/node/test_resolver.h | 12 ++++++++++-- test/routing/test_routing.h | 6 +++++- 6 files changed, 32 insertions(+), 26 deletions(-) diff --git a/importer/importer.c b/importer/importer.c index bcd4945..136956a 100644 --- a/importer/importer.c +++ b/importer/importer.c @@ -6,6 +6,7 @@ #include "ipfs/importer/importer.h" #include "ipfs/merkledag/merkledag.h" #include "libp2p/os/utils.h" +#include "ipfs/cmd/cli.h" #include "ipfs/core/ipfs_node.h" #include "ipfs/repo/fsrepo/fs_repo.h" #include "ipfs/repo/init.h" @@ -312,26 +313,17 @@ int ipfs_import_file(const char* root_dir, const char* fileName, struct Hashtabl * @param argv command line parameters * @returns a FileList linked list of filenames */ -struct FileList* ipfs_import_get_filelist(int argc, char** argv) { +struct FileList* ipfs_import_get_filelist(struct CliArguments* args) { struct FileList* first = NULL; struct FileList* last = NULL; - int skipNext = 0; - for (int i = 2; i < argc; i++) { - if (skipNext) { - skipNext = 0; - continue; - } - if (strcmp(argv[i], "-r") == 0) { - continue; - } - if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) { - skipNext = 1; + for (int i = args->verb_index + 1; i < args->argc; i++) { + if (strcmp(args->argv[i], "add") == 0) { continue; } struct FileList* current = (struct FileList*)malloc(sizeof(struct FileList)); current->next = NULL; - current->file_name = argv[i]; + current->file_name = args->argv[i]; // now wire it in if (first == NULL) { first = current; @@ -364,7 +356,7 @@ int ipfs_import_is_recursive(int argc, char** argv) { * @param argc the number of arguments * @param argv the arguments */ -int ipfs_import_files(int argc, char** argv) { +int ipfs_import_files(struct CliArguments* args) { /* * Param 0: ipfs * param 1: add @@ -380,17 +372,17 @@ int ipfs_import_files(int argc, char** argv) { char* filename = NULL; struct HashtableNode* directory_entry = NULL; - int recursive = ipfs_import_is_recursive(argc, argv); + int recursive = ipfs_import_is_recursive(args->argc, args->argv); // parse the command line - first = ipfs_import_get_filelist(argc, argv); + first = ipfs_import_get_filelist(args); // open the repo - if (!ipfs_repo_get_directory(argc, argv, &repo_path)) { + if (!ipfs_repo_get_directory(args->argc, args->argv, &repo_path)) { fprintf(stderr, "Repo does not exist: %s\n", repo_path); goto exit; } - ipfs_node_online_new(repo_path, &local_node); + ipfs_node_offline_new(repo_path, &local_node); // import the file(s) @@ -434,8 +426,8 @@ int ipfs_import_files(int argc, char** argv) { free(filename); if (directory_entry != NULL) ipfs_hashtable_node_free(directory_entry); - if (repo_path != NULL) - free(repo_path); + //if (repo_path != NULL) + // free(repo_path); return retVal; } diff --git a/include/ipfs/importer/importer.h b/include/ipfs/importer/importer.h index 18decd6..d4351de 100644 --- a/include/ipfs/importer/importer.h +++ b/include/ipfs/importer/importer.h @@ -1,6 +1,7 @@ #ifndef __IPFS_IMPORTER_IMPORTER_H__ #define __IPFS_IMPORTER_IMPORTER_H__ +#include "ipfs/cmd/cli.h" #include "ipfs/merkledag/node.h" #include "ipfs/core/ipfs_node.h" @@ -26,6 +27,6 @@ int ipfs_import_file(const char* root, const char* fileName, struct HashtableNod * @param argc the number of arguments * @param argv the arguments */ -int ipfs_import_files(int argc, char** argv); +int ipfs_import_files(struct CliArguments* args); #endif /* INCLUDE_IPFS_IMPORTER_IMPORTER_H_ */ diff --git a/main/main.c b/main/main.c index c6641ab..3c73210 100644 --- a/main/main.c +++ b/main/main.c @@ -162,7 +162,7 @@ int main(int argc, char** argv) { return ipfs_repo_init(argc, argv); break; case (ADD): - ipfs_import_files(argc, argv); + ipfs_import_files(args); break; case (OBJECT_GET): ipfs_exporter_object_get(argc, argv); diff --git a/test/core/test_api.h b/test/core/test_api.h index 854bad6..c57f198 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -63,6 +63,7 @@ int test_core_api_object_cat() { struct FSRepo* fs_repo = NULL; char hash[256] = ""; char* args[] = {"ipfs", "--config", ipfs_path2, "cat", hash }; + struct CliArguments* arguments = NULL; // logging libp2p_logger_add_class("test_api"); @@ -126,7 +127,7 @@ int test_core_api_object_cat() { sleep(3); // use a client to ask for the file on server 1 - struct CliArguments* arguments = cli_arguments_new(5, args); + arguments = cli_arguments_new(5, args); if (ipfs_exporter_object_cat(arguments) == 0) { libp2p_logger_error("test_api", "ipfs_exporter_object_cat returned false.\n"); goto exit; diff --git a/test/node/test_resolver.h b/test/node/test_resolver.h index be5ee54..237e05a 100644 --- a/test/node/test_resolver.h +++ b/test/node/test_resolver.h @@ -1,5 +1,6 @@ #include +#include "ipfs/cmd/cli.h" #include "ipfs/importer/resolver.h" #include "libp2p/os/utils.h" #include "multiaddr/multiaddr.h" @@ -15,6 +16,7 @@ int test_resolver_get() { char* argv[argc]; const char* work_path = "/tmp"; char ipfs_path[12]; + struct CliArguments* arguments = NULL; sprintf(&ipfs_path[0], "%s/%s", work_path, ".ipfs"); os_utils_filepath_join(home_dir, "ipfstest", test_dir, strlen(home_dir) + 10); @@ -30,7 +32,8 @@ int test_resolver_get() { argv[4] = "-c"; argv[5] = (char*)work_path; - ipfs_import_files(argc, (char**)argv); + arguments = cli_arguments_new(6, (char**)&argv); + ipfs_import_files(arguments); ipfs_repo_fsrepo_new(ipfs_path, NULL, &fs_repo); ipfs_repo_fsrepo_open(fs_repo); @@ -73,6 +76,7 @@ int test_resolver_get() { free(test_dir); ipfs_repo_fsrepo_free(fs_repo); ipfs_hashtable_node_free(result); + cli_arguments_free(arguments); return retVal; } @@ -87,6 +91,7 @@ int test_resolver_remote_get() { int retVal = 0; struct FSRepo* fs_repo = NULL; struct HashtableNode* result = NULL; + struct CliArguments* arguments; // this should point to a test directory with files and directories char* home_dir = os_utils_get_homedir(); @@ -99,6 +104,8 @@ int test_resolver_remote_get() { argv[2] = "-r"; argv[3] = test_dir; + arguments = cli_arguments_new(4, (char**)&argv); + drop_and_build_repository(ipfs_path, 4001, NULL, NULL); // start the daemon in a separate thread @@ -108,7 +115,7 @@ int test_resolver_remote_get() { os_utils_filepath_join(home_dir, "ipfstest", test_dir, strlen(home_dir) + 10); - ipfs_import_files(argc, (char**)argv); + ipfs_import_files(arguments); ipfs_repo_fsrepo_new(ipfs_path, NULL, &fs_repo); ipfs_repo_fsrepo_open(fs_repo); @@ -149,6 +156,7 @@ int test_resolver_remote_get() { ipfs_repo_fsrepo_free(fs_repo); if (local_node.peerstore != NULL) libp2p_peerstore_free(local_node.peerstore); + cli_arguments_free(arguments); return retVal; } diff --git a/test/routing/test_routing.h b/test/routing/test_routing.h index dbfe338..7d284a6 100644 --- a/test/routing/test_routing.h +++ b/test/routing/test_routing.h @@ -8,6 +8,7 @@ #include "multiaddr/multiaddr.h" +#include "ipfs/cmd/cli.h" #include "ipfs/core/daemon.h" #include "ipfs/core/ipfs_node.h" #include "ipfs/routing/routing.h" @@ -32,6 +33,7 @@ int test_routing_put_value() { char* peer_id_consumer = NULL; int consumer_thread_started = 0; struct Libp2pVector* ma_vector = NULL; + struct CliArguments* arguments = NULL; // fire up the "publisher" drop_and_build_repository(ipfs_path_publisher, 4001, NULL, &peer_id_publisher); @@ -39,7 +41,8 @@ int test_routing_put_value() { sprintf(multiaddress_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s", peer_id_publisher); ma_publisher = multiaddress_new_from_string(multiaddress_string); char* args[] = { "ipfs", "--config", ipfs_path_publisher, "add", "-r", "~/site"}; - ipfs_import_files(6, args); + arguments = cli_arguments_new(6, args); + ipfs_import_files(arguments); if (!pthread_create(&thread_publisher, NULL, test_daemon_start, (void*)ipfs_path_publisher)) { goto exit; } @@ -82,6 +85,7 @@ int test_routing_put_value() { pthread_join(thread_consumer, NULL); } multiaddress_free(ma_publisher); + cli_arguments_free(arguments); return retVal; }