From e766ea94290ec1ffa09ee711a3e54389a5b2deb7 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 5 Mar 2018 14:37:14 -0500 Subject: [PATCH 01/10] correctly handle list of methods --- src/rpc/cli.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index caf85b7..3f0b065 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -114,20 +114,20 @@ void cli::run() /**** * @brief loop through list of commands, attempting to find a match * @param token what the user typed - * @param match - * @returns the full name of the command or NULL if no matches found + * @param match sets to 1 if only 1 match was found + * @returns the remaining letters of the name of the command or NULL if 1 match not found */ static char *my_rl_complete(char *token, int *match) { int matchlen = 0; int count = 0; - const char* method_name; + std::string method_name; auto& cmd = cli_commands(); int partlen = strlen (token); /* Part of token */ - for (auto it : cmd) { - if (!strncmp (it.c_str(), token, partlen)) { - method_name = it.c_str(); + for (std::string it : cmd) { + if (!strncmp ( it.c_str(), token, partlen)) { + method_name = it; matchlen = partlen; count ++; } @@ -135,7 +135,7 @@ static char *my_rl_complete(char *token, int *match) if (count == 1) { *match = 1; - return strdup (method_name + matchlen); + return strdup (method_name.c_str() + matchlen); } return NULL; From 5f3ace5ca31088f26e56c1cbe7353eabcb607a43 Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 6 Mar 2018 08:06:18 -0500 Subject: [PATCH 02/10] Added space at end of completed command --- src/rpc/cli.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index 3f0b065..b585bc2 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -126,16 +126,17 @@ static char *my_rl_complete(char *token, int *match) auto& cmd = cli_commands(); int partlen = strlen (token); /* Part of token */ for (std::string it : cmd) { - if (!strncmp ( it.c_str(), token, partlen)) { - method_name = it; - matchlen = partlen; - count ++; - } + if (!strncmp ( it.c_str(), token, partlen)) { + method_name = it; + matchlen = partlen; + count ++; + } } if (count == 1) { - *match = 1; - return strdup (method_name.c_str() + matchlen); + *match = 1; + method_name += " "; + return strdup (method_name.c_str() + matchlen); } return NULL; From f5d68e933652aa5a3395fe6fb1762e41cb664ae7 Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 6 Mar 2018 09:25:38 -0500 Subject: [PATCH 03/10] Adjusting for formatting guidelines --- src/rpc/cli.cpp | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index b585bc2..f85b7fd 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -119,27 +119,30 @@ void cli::run() */ static char *my_rl_complete(char *token, int *match) { - int matchlen = 0; - int count = 0; - std::string method_name; + int matchlen = 0; + int count = 0; + std::string method_name; - auto& cmd = cli_commands(); - int partlen = strlen (token); /* Part of token */ - for (std::string it : cmd) { - if (!strncmp ( it.c_str(), token, partlen)) { - method_name = it; - matchlen = partlen; - count ++; - } - } + auto& cmd = cli_commands(); + int partlen = strlen (token); /* Part of token */ + for (const std::string it : cmd) + { + if (it.compare(0, partlen, token) == 0) + { + method_name = it; + matchlen = partlen; + count ++; + } + } - if (count == 1) { - *match = 1; - method_name += " "; - return strdup (method_name.c_str() + matchlen); - } + if (count == 1) + { + *match = 1; + method_name += " "; + return strdup (method_name.c_str() + matchlen); + } - return NULL; + return NULL; } /*** From 1fe7d4be46cf11c346fc35d24571e6829a9966ca Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 6 Mar 2018 11:44:00 -0500 Subject: [PATCH 04/10] Trying to get the #$%@$ tabs to disappear --- src/rpc/cli.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index f85b7fd..8687e0b 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -119,30 +119,30 @@ void cli::run() */ static char *my_rl_complete(char *token, int *match) { - int matchlen = 0; - int count = 0; - std::string method_name; + int matchlen = 0; + int count = 0; + std::string method_name; - auto& cmd = cli_commands(); - int partlen = strlen (token); /* Part of token */ - for (const std::string it : cmd) - { - if (it.compare(0, partlen, token) == 0) - { - method_name = it; - matchlen = partlen; - count ++; - } - } + auto& cmd = cli_commands(); + int partlen = strlen (token); /* Part of token */ + for (const std::string it : cmd) + { + if (it.compare(0, partlen, token) == 0) + { + method_name = it; + matchlen = partlen; + count ++; + } + } - if (count == 1) - { - *match = 1; - method_name += " "; - return strdup (method_name.c_str() + matchlen); - } + if (count == 1) + { + *match = 1; + method_name += " "; + return strdup (method_name.c_str() + matchlen); + } - return NULL; + return NULL; } /*** From c225488cd4070a9e3df8cbf06914b327dab91608 Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 6 Mar 2018 17:17:09 -0500 Subject: [PATCH 05/10] replace tabs with spaces --- src/rpc/cli.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index 8687e0b..22ef34b 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -153,22 +153,22 @@ static char *my_rl_complete(char *token, int *match) */ static int cli_completion(char *token, char ***av) { - int num, total = 0; - char **copy; + int num, total = 0; + char **copy; - auto& cmd = cli_commands(); - num = cmd.size(); + auto& cmd = cli_commands(); + num = cmd.size(); - copy = (char **) malloc (num * sizeof(char *)); - for (auto it : cmd) { - if (!strncmp (it.c_str(), token, strlen (token))) { - copy[total] = strdup ( it.c_str() ); - total ++; - } - } - *av = copy; + copy = (char **) malloc (num * sizeof(char *)); + for (auto it : cmd) { + if (!strncmp (it.c_str(), token, strlen (token))) { + copy[total] = strdup ( it.c_str() ); + total ++; + } + } + *av = copy; - return total; + return total; } /*** From b3053d04217c6b47b7511b0791a52e46183b3290 Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 7 Mar 2018 02:55:59 -0500 Subject: [PATCH 06/10] adjust spacing, add const, remove unnecessary variable, bad malloc check --- src/rpc/cli.cpp | 54 +++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index 22ef34b..fdb3a25 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -119,27 +119,33 @@ void cli::run() */ static char *my_rl_complete(char *token, int *match) { - int matchlen = 0; - int count = 0; + bool have_one = false; std::string method_name; auto& cmd = cli_commands(); int partlen = strlen (token); /* Part of token */ + for (const std::string it : cmd) { if (it.compare(0, partlen, token) == 0) { - method_name = it; - matchlen = partlen; - count ++; + if (have_one) { + // we can only have 1, but we found a second + return NULL; + } + else + { + method_name = it; + have_one = true; + } } } - if (count == 1) + if (have_one) { *match = 1; method_name += " "; - return strdup (method_name.c_str() + matchlen); + return strdup (method_name.c_str() + partlen); } return NULL; @@ -148,27 +154,35 @@ static char *my_rl_complete(char *token, int *match) /*** * @brief return an array of matching commands * @param token the incoming text - * @param av the resultant array of possible matches + * @param array the resultant array of possible matches * @returns the number of matches */ -static int cli_completion(char *token, char ***av) +static int cli_completion(char *token, char ***array) { - int num, total = 0; - char **copy; - auto& cmd = cli_commands(); - num = cmd.size(); + int num_commands = cmd.size(); - copy = (char **) malloc (num * sizeof(char *)); - for (auto it : cmd) { - if (!strncmp (it.c_str(), token, strlen (token))) { - copy[total] = strdup ( it.c_str() ); - total ++; + char **copy = (char **) malloc (num_commands * sizeof(char *)); + if (copy == NULL) + { + // possible out of memory + return 0; + } + int total_matches = 0; + + int partlen = strlen(token); + + for (const std::string it : cmd) + { + if ( it.compare(0, partlen, token) == 0) + { + copy[total_matches] = strdup ( it.c_str() ); + ++total_matches; } } - *av = copy; + *array = copy; - return total; + return total_matches; } /*** From 5b5190a55c16ae852020aa53f390383ab5e8b69f Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 7 Mar 2018 09:44:35 -0500 Subject: [PATCH 07/10] changed const to const ref --- src/rpc/cli.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index fdb3a25..993e19f 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -125,7 +125,7 @@ static char *my_rl_complete(char *token, int *match) auto& cmd = cli_commands(); int partlen = strlen (token); /* Part of token */ - for (const std::string it : cmd) + for (const std::string& it : cmd) { if (it.compare(0, partlen, token) == 0) { @@ -172,7 +172,7 @@ static int cli_completion(char *token, char ***array) int partlen = strlen(token); - for (const std::string it : cmd) + for (const std::string& it : cmd) { if ( it.compare(0, partlen, token) == 0) { From 43b81cb69bd8f93922ac0f8773be7ee3fd2ffcae Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 8 Mar 2018 07:40:20 -0500 Subject: [PATCH 08/10] assuring that malloc gets freed --- src/rpc/cli.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index 993e19f..2570c9f 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -216,10 +216,20 @@ void cli::getline( const fc::string& prompt, fc::string& line) line_read = readline(prompt.c_str()); if( line_read == nullptr ) FC_THROW_EXCEPTION( fc::eof_exception, "" ); - if( *line_read ) - add_history(line_read); line = line_read; - free(line_read); + if (*line_read) + { + try + { + add_history(line_read); + free(line_read); + } + catch(...) + { + free(line_read); + throw; + } + } }).wait(); } else From 2017ed911a9a1e62fd0b824478ea063ac9ce60a4 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 8 Mar 2018 10:46:23 -0500 Subject: [PATCH 09/10] fix memory leak --- src/rpc/cli.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index 2570c9f..29d5051 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -217,18 +217,16 @@ void cli::getline( const fc::string& prompt, fc::string& line) if( line_read == nullptr ) FC_THROW_EXCEPTION( fc::eof_exception, "" ); line = line_read; - if (*line_read) + try { - try - { + if (*line_read) add_history(line_read); - free(line_read); - } - catch(...) - { - free(line_read); - throw; - } + free(line_read); + } + catch(...) + { + free(line_read); + throw; } }).wait(); } From 9ee2bcf0a5f44b8f414f893bb3d001ec8abb97c8 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 8 Mar 2018 11:05:34 -0500 Subject: [PATCH 10/10] avoid double free --- src/rpc/cli.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index 29d5051..c4030f8 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -221,13 +221,13 @@ void cli::getline( const fc::string& prompt, fc::string& line) { if (*line_read) add_history(line_read); - free(line_read); } catch(...) { free(line_read); throw; } + free(line_read); }).wait(); } else