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.
diff --git a/src/standard.c b/src/standard.c
index f4f8b2a..75a0389 100644
--- a/src/standard.c
+++ b/src/standard.c
@@ -2038,10 +2038,11 @@
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 @@
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;
}