mirror of
https://github.com/haproxy/haproxy.git
synced 2026-03-06 23:31:08 -05: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
1 changed files with 6 additions and 5 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue