BUILD/MEDIUM: standard: get rid of sprintf()

OpenBSD complains about the use of sprintf in human_time() :

src/standard.o(.text+0x1c40): In function `human_time':
src/standard.c:2067: warning: sprintf() is often misused, please use snprintf()

We can easily get around this by having a pointer to the end of the string and
using snprintf() instead.
This commit is contained in:
Willy Tarreau 2014-04-14 14:53:06 +02:00
parent 94ef3f3115
commit 761b3d557e

View File

@ -2038,10 +2038,11 @@ int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
char *human_time(int t, short hz_div) {
static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
char *p = rv;
char *end = rv + sizeof(rv);
int cnt=2; // print two numbers
if (unlikely(t < 0 || hz_div <= 0)) {
sprintf(p, "?");
snprintf(p, end - p, "?");
return rv;
}
@ -2049,22 +2050,22 @@ char *human_time(int t, short hz_div) {
t /= hz_div;
if (t >= DAY) {
p += sprintf(p, "%dd", t / DAY);
p += snprintf(p, end - p, "%dd", t / DAY);
cnt--;
}
if (cnt && t % DAY / HOUR) {
p += sprintf(p, "%dh", t % DAY / HOUR);
p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
cnt--;
}
if (cnt && t % HOUR / MINUTE) {
p += sprintf(p, "%dm", t % HOUR / MINUTE);
p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
cnt--;
}
if ((cnt && t % MINUTE) || !t) // also display '0s'
p += sprintf(p, "%ds", t % MINUTE / SEC);
p += snprintf(p, end - p, "%ds", t % MINUTE / SEC);
return rv;
}