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 9 Jul 2004 09:41:21 -0000 @@ -180,6 +180,8 @@ static char *format_uptimestr(time_t tim { int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0; char timestr[256]; + int bytes = 0; + int maxbytes = 0; int pos = 0; #define SECOND (1) #define MINUTE (SECOND*60) @@ -188,53 +190,64 @@ static char *format_uptimestr(time_t tim #define WEEK (DAY*7) #define YEAR (DAY*365) + maxbytes = sizeof(timestr); if (timeval < 0) return NULL; if (timeval > YEAR) { years = (timeval / YEAR); timeval -= (years * YEAR); if (years > 1) - pos += sprintf(timestr + pos, "%d years, ", years); + bytes = snprintf(timestr, maxbytes, "%d years, ", years); else - pos += sprintf(timestr + pos, "1 year, "); + bytes = snprintf(timestr, maxbytes, "1 year, "); + pos += bytes; + maxbytes -= bytes; } if (timeval > WEEK) { weeks = (timeval / WEEK); timeval -= (weeks * WEEK); if (weeks > 1) - pos += sprintf(timestr + pos, "%d weeks, ", weeks); + bytes = snprintf(timestr + pos, maxbytes, "%d weeks, ", weeks); else - pos += sprintf(timestr + pos, "1 week, "); + bytes = snprintf(timestr + pos, maxbytes, "1 week, "); + pos += bytes; + maxbytes -= bytes; } if (timeval > DAY) { days = (timeval / DAY); timeval -= (days * DAY); if (days > 1) - pos += sprintf(timestr + pos, "%d days, ", days); + bytes = snprintf(timestr + pos, maxbytes, "%d days, ", days); else - pos += sprintf(timestr + pos, "1 day, "); - + bytes = snprintf(timestr + pos, maxbytes, "1 day, "); + pos += bytes; + maxbytes -= bytes; } if (timeval > HOUR) { hours = (timeval / HOUR); timeval -= (hours * HOUR); if (hours > 1) - pos += sprintf(timestr + pos, "%d hours, ", hours); + bytes = snprintf(timestr + pos, maxbytes, "%d hours, ", hours); else - pos += sprintf(timestr + pos, "1 hour, "); + bytes = snprintf(timestr + pos, maxbytes, "1 hour, "); + pos += bytes; + maxbytes -= bytes; } if (timeval > MINUTE) { mins = (timeval / MINUTE); timeval -= (mins * MINUTE); + bytes = 0; if (mins > 1) - pos += sprintf(timestr + pos, "%d minutes, ", mins); + bytes = snprintf(timestr + pos, maxbytes, "%d minutes, ", mins); else if (mins > 0) - pos += sprintf(timestr + pos, "1 minute, "); + bytes = snprintf(timestr + pos, maxbytes, "1 minute, "); + pos += bytes; + maxbytes -= bytes; } secs = timeval; if (secs > 0) - pos += sprintf(timestr + pos, "%d seconds", secs); + snprintf(timestr + pos, maxbytes, "%d seconds", secs); return timestr ? strdup(timestr) : NULL; } @@ -664,8 +677,8 @@ static void join(char *s, int len, char strcpy(s, ""); for (x=0;w[x];x++) { if (x) - strncat(s, " ", len - strlen(s)); - strncat(s, w[x], len - strlen(s)); + strncat(s, " ", len - strlen(s) - 1); + strncat(s, w[x], len - strlen(s) - 1); } } @@ -675,7 +688,7 @@ static void join2(char *s, int len, char /* Join words into a string */ strcpy(s, ""); for (x=0;w[x];x++) { - strncat(s, w[x], len - strlen(s)); + strncat(s, w[x], len - strlen(s) - 1); } }