summaryrefslogtreecommitdiff
path: root/main/cli.c
diff options
context:
space:
mode:
authorRichard Mudgett <rmudgett@digium.com>2013-09-10 18:05:47 +0000
committerRichard Mudgett <rmudgett@digium.com>2013-09-10 18:05:47 +0000
commit83bf017db9804c9274608ded72d70a72c086d756 (patch)
treea2f9a38495c75235101c86a3572529098fc5800f /main/cli.c
parent87cf916cdbcf16d244bd71d91ec5b849cc186923 (diff)
Fix incorrect usages of ast_realloc().
There are several locations in the code base where this is done: buf = ast_realloc(buf, new_size); This is going to leak the original buf contents if the realloc fails. Review: https://reviewboard.asterisk.org/r/2832/ ........ Merged revisions 398757 from http://svn.asterisk.org/svn/asterisk/branches/1.8 ........ Merged revisions 398758 from http://svn.asterisk.org/svn/asterisk/branches/11 ........ Merged revisions 398759 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@398760 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/cli.c')
-rw-r--r--main/cli.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/main/cli.c b/main/cli.c
index 54efd6f4c..9d9fda4f1 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -2363,9 +2363,22 @@ int ast_cli_generatornummatches(const char *text, const char *word)
return matches;
}
+static void destroy_match_list(char **match_list, int matches)
+{
+ if (match_list) {
+ int idx;
+
+ for (idx = 1; idx < matches; ++idx) {
+ ast_free(match_list[idx]);
+ }
+ ast_free(match_list);
+ }
+}
+
char **ast_cli_completion_matches(const char *text, const char *word)
{
char **match_list = NULL, *retstr, *prevstr;
+ char **new_list;
size_t match_list_len, max_equal, which, i;
int matches = 0;
@@ -2374,14 +2387,19 @@ char **ast_cli_completion_matches(const char *text, const char *word)
while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
if (matches + 1 >= match_list_len) {
match_list_len <<= 1;
- if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list))))
+ new_list = ast_realloc(match_list, match_list_len * sizeof(*match_list));
+ if (!new_list) {
+ destroy_match_list(match_list, matches);
return NULL;
+ }
+ match_list = new_list;
}
match_list[++matches] = retstr;
}
- if (!match_list)
+ if (!match_list) {
return match_list; /* NULL */
+ }
/* Find the longest substring that is common to all results
* (it is a candidate for completion), and store a copy in entry 0.
@@ -2394,20 +2412,23 @@ char **ast_cli_completion_matches(const char *text, const char *word)
max_equal = i;
}
- if (!(retstr = ast_malloc(max_equal + 1))) {
- ast_free(match_list);
+ retstr = ast_malloc(max_equal + 1);
+ if (!retstr) {
+ destroy_match_list(match_list, matches);
return NULL;
}
-
ast_copy_string(retstr, match_list[1], max_equal + 1);
match_list[0] = retstr;
/* ensure that the array is NULL terminated */
if (matches + 1 >= match_list_len) {
- if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list)))) {
+ new_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list));
+ if (!new_list) {
ast_free(retstr);
+ destroy_match_list(match_list, matches);
return NULL;
}
+ match_list = new_list;
}
match_list[matches + 1] = NULL;