Index: cli.c =================================================================== RCS file: /usr/cvsroot/asterisk/cli.c,v retrieving revision 1.45 diff -u -p -r1.45 cli.c --- cli.c 8 Jul 2004 19:41:34 -0000 1.45 +++ cli.c 10 Jul 2004 05:16:38 -0000 @@ -39,12 +39,19 @@ void ast_cli(int fd, char *fmt, ...) { char *stuff; + int res = 0; + va_list ap; va_start(ap, fmt); - vasprintf(&stuff, fmt, ap); + res = vasprintf(&stuff, fmt, ap); va_end(ap); - ast_carefulwrite(fd, stuff, strlen(stuff), 100); - free(stuff); + if (res == -1) { + ast_log(LOG_ERROR, "Out of memory\n"); + } + else { + ast_carefulwrite(fd, stuff, strlen(stuff), 100); + free(stuff); + } } AST_MUTEX_DEFINE_STATIC(clilock); @@ -179,8 +186,8 @@ static char version_help[] = static char *format_uptimestr(time_t timeval) { int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0; - char timestr[256]; - int pos = 0; + char timestr[256]=""; + char buff[80]=""; #define SECOND (1) #define MINUTE (SECOND*60) #define HOUR (MINUTE*60) @@ -194,47 +201,56 @@ static char *format_uptimestr(time_t tim years = (timeval / YEAR); timeval -= (years * YEAR); if (years > 1) - pos += sprintf(timestr + pos, "%d years, ", years); + snprintf(timestr, sizeof(timestr), "%d years, ", years); else - pos += sprintf(timestr + pos, "1 year, "); + strncpy(timestr, "1 year, ", sizeof(timestr) - 1); } if (timeval > WEEK) { weeks = (timeval / WEEK); timeval -= (weeks * WEEK); - if (weeks > 1) - pos += sprintf(timestr + pos, "%d weeks, ", weeks); + if (weeks > 1) { + snprintf(buff, sizeof(buff), "%d weeks, ", weeks); + strncat(timestr, buff, sizeof(timestr) - strlen(timestr) - 1); + } else - pos += sprintf(timestr + pos, "1 week, "); + strncat(timestr, "1 week, ", sizeof(timestr) - strlen(timestr) - 1); } if (timeval > DAY) { days = (timeval / DAY); timeval -= (days * DAY); - if (days > 1) - pos += sprintf(timestr + pos, "%d days, ", days); + if (days > 1) { + snprintf(buff, sizeof(buff), "%d days, ", days); + strncat(timestr, buff, sizeof(timestr) - strlen(timestr) - 1); + } else - pos += sprintf(timestr + pos, "1 day, "); - + strncat(timestr, "1 day, ", sizeof(timestr) - strlen(timestr) - 1); } if (timeval > HOUR) { hours = (timeval / HOUR); timeval -= (hours * HOUR); - if (hours > 1) - pos += sprintf(timestr + pos, "%d hours, ", hours); + if (hours > 1) { + snprintf(buff, sizeof(buff), "%d hours, ", hours); + strncat(timestr, buff, sizeof(timestr) - strlen(timestr) - 1); + } else - pos += sprintf(timestr + pos, "1 hour, "); + strncat(timestr, "1 hour, ", sizeof(timestr) - strlen(timestr) - 1); } if (timeval > MINUTE) { mins = (timeval / MINUTE); timeval -= (mins * MINUTE); - if (mins > 1) - pos += sprintf(timestr + pos, "%d minutes, ", mins); + if (mins > 1) { + snprintf(buff, sizeof(buff), "%d minutes, ", mins); + strncat(timestr, buff, sizeof(timestr) - strlen(timestr) - 1); + } else if (mins > 0) - pos += sprintf(timestr + pos, "1 minute, "); + strncat(timestr, "1 minute, ", sizeof(timestr) - strlen(timestr) - 1); } secs = timeval; - if (secs > 0) - pos += sprintf(timestr + pos, "%d seconds", secs); + if (secs > 0) { + snprintf(buff, sizeof(buff), "%d seconds", secs); + strncat(timestr, buff, sizeof(timestr) - strlen(timestr) - 1); + } return timestr ? strdup(timestr) : NULL; } @@ -657,25 +673,31 @@ static struct ast_cli_entry *find_cli(ch return e; } -static void join(char *s, int len, char *w[]) +static void join(char *dest, size_t destsize, char *w[]) { int x; /* Join words into a string */ - strcpy(s, ""); + if (!dest || destsize < 1) { + return; + } + dest[0] = '\0'; for (x=0;w[x];x++) { if (x) - strncat(s, " ", len - strlen(s)); - strncat(s, w[x], len - strlen(s)); + strncat(dest, " ", destsize - strlen(dest) - 1); + strncat(dest, w[x], destsize - strlen(dest) - 1); } } -static void join2(char *s, int len, char *w[]) +static void join2(char *dest, size_t destsize, char *w[]) { int x; /* Join words into a string */ - strcpy(s, ""); + if (!dest || destsize < 1) { + return; + } + dest[0] = '\0'; for (x=0;w[x];x++) { - strncat(s, w[x], len - strlen(s)); + strncat(dest, w[x], destsize - strlen(dest) - 1); } }