mfiutil: Handle truncation in mfi_next_learn_time

Only trim the trailing newline if the output from ctime() was not
truncated.  To simplify the code, use strlcpy() instead of snprintf()
since strlcpy() can't fail with a negative size (and the previous code
probably didn't handle those errors properly given the int -> size_t
conversion).

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D50882
This commit is contained in:
John Baldwin committed 2025-07-07 12:37:09 -04:00
1 parent fe1cd734a1
commit 10ed5b90e4
1 file changed
+2 -2
+2 -2
View File
@@ -71,8 +71,8 @@ mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
tm.tm_year = 100;
basetime = timegm(&tm);
basetime += (time_t)next_learn_time;
len = snprintf(buf, sz, "%s", ctime(&basetime));
if (len > 0)
len = strlcpy(buf, ctime(&basetime), sz);
if (len < sz)
/* Get rid of the newline added by ctime(3). */
buf[len - 1] = '\0';
}