aboutsummaryrefslogtreecommitdiffstats
path: root/cli.c
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-02-09 16:59:50 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-02-09 16:59:50 +0000
commit34490de229310cffd8ee332e86e9e1018aaa57f5 (patch)
treebc582fc8d1412250a1a150dcaf06bfb7fe76e0c2 /cli.c
parent0a2b114fb5907f90ec49f443931fffdb79a89d55 (diff)
conversions to memory allocation wrappers, remove duplicated error messages,
remove unnecessary casts, malloc+memset to calloc (issue #6395) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@9310 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'cli.c')
-rw-r--r--cli.c36
1 files changed, 19 insertions, 17 deletions
diff --git a/cli.c b/cli.c
index 53b7b1a6c..fcdb41779 100644
--- a/cli.c
+++ b/cli.c
@@ -63,14 +63,14 @@ extern unsigned long global_fin, global_fout;
void ast_cli(int fd, char *fmt, ...)
{
char *stuff;
- int res = 0;
+ int res;
va_list ap;
va_start(ap, fmt);
res = vasprintf(&stuff, fmt, ap);
va_end(ap);
if (res == -1) {
- ast_log(LOG_ERROR, "Out of memory\n");
+ ast_log(LOG_ERROR, "Memory allocation failure\n");
} else {
ast_carefulwrite(fd, stuff, strlen(stuff), 100);
free(stuff);
@@ -520,8 +520,7 @@ static int handle_commandmatchesarray(int fd, int argc, char *argv[])
if (argc != 4)
return RESULT_SHOWUSAGE;
- buf = malloc(buflen);
- if (!buf)
+ if (!(buf = ast_malloc(buflen)))
return RESULT_FAILURE;
buf[len] = '\0';
matches = ast_cli_completion_matches(argv[2], argv[3]);
@@ -534,9 +533,8 @@ static int handle_commandmatchesarray(int fd, int argc, char *argv[])
if (len + matchlen >= buflen) {
buflen += matchlen * 3;
obuf = buf;
- buf = realloc(obuf, buflen);
- if (!buf)
- /* Out of memory... Just free old buffer and be done */
+ if (!(buf = ast_realloc(obuf, buflen)))
+ /* Memory allocation failure... Just free old buffer and be done */
free(obuf);
}
if (buf)
@@ -1274,13 +1272,14 @@ 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;
- match_list = realloc(match_list, match_list_len * sizeof(char *));
+ if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list))))
+ return NULL;
}
match_list[++matches] = retstr;
}
if (!match_list)
- return (char **) NULL;
+ return NULL;
which = 2;
prevstr = match_list[1];
@@ -1291,14 +1290,18 @@ char **ast_cli_completion_matches(const char *text, const char *word)
max_equal = i;
}
- retstr = malloc(max_equal + 1);
+ if (!(retstr = ast_malloc(max_equal + 1)))
+ return NULL;
+
strncpy(retstr, match_list[1], max_equal);
retstr[max_equal] = '\0';
match_list[0] = retstr;
- if (matches + 1 >= match_list_len)
- match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
- match_list[matches + 1] = (char *) NULL;
+ if (matches + 1 >= match_list_len) {
+ if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list))))
+ return NULL;
+ }
+ match_list[matches + 1] = NULL;
return match_list;
}
@@ -1391,10 +1394,9 @@ int ast_cli_command(int fd, const char *s)
int x;
char *dup;
int tws;
-
- dup = parse_args(s, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws);
- if (!dup) {
- ast_log(LOG_ERROR, "Out of Memory!\n");
+
+ if (!(dup = parse_args(s, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws))) {
+ ast_log(LOG_ERROR, "Memory allocation failure\n");
return -1;
}