]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD/MEDIUM: standard: get rid of sprintf()
authorWilly Tarreau <w@1wt.eu>
Mon, 14 Apr 2014 12:53:06 +0000 (14:53 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 14 Apr 2014 13:52:48 +0000 (15:52 +0200)
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.

src/standard.c

index f4f8b2af178907322a430201289b9f9d492cc077..75a0389b8c64a315bc39f6f168b8fecb5d219adc 100644 (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;
 }