mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-08-09 08:37:04 +02:00
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:
parent
94ef3f3115
commit
761b3d557e
@ -2038,10 +2038,11 @@ int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
|
|||||||
char *human_time(int t, short hz_div) {
|
char *human_time(int t, short hz_div) {
|
||||||
static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
|
static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
|
||||||
char *p = rv;
|
char *p = rv;
|
||||||
|
char *end = rv + sizeof(rv);
|
||||||
int cnt=2; // print two numbers
|
int cnt=2; // print two numbers
|
||||||
|
|
||||||
if (unlikely(t < 0 || hz_div <= 0)) {
|
if (unlikely(t < 0 || hz_div <= 0)) {
|
||||||
sprintf(p, "?");
|
snprintf(p, end - p, "?");
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2049,22 +2050,22 @@ char *human_time(int t, short hz_div) {
|
|||||||
t /= hz_div;
|
t /= hz_div;
|
||||||
|
|
||||||
if (t >= DAY) {
|
if (t >= DAY) {
|
||||||
p += sprintf(p, "%dd", t / DAY);
|
p += snprintf(p, end - p, "%dd", t / DAY);
|
||||||
cnt--;
|
cnt--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cnt && t % DAY / HOUR) {
|
if (cnt && t % DAY / HOUR) {
|
||||||
p += sprintf(p, "%dh", t % DAY / HOUR);
|
p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
|
||||||
cnt--;
|
cnt--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cnt && t % HOUR / MINUTE) {
|
if (cnt && t % HOUR / MINUTE) {
|
||||||
p += sprintf(p, "%dm", t % HOUR / MINUTE);
|
p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
|
||||||
cnt--;
|
cnt--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((cnt && t % MINUTE) || !t) // also display '0s'
|
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;
|
return rv;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user