Skip to content

Commit aae67a2

Browse files
Low-powerbsdimp
authored andcommitted
mfiutil: Fix unsafe assumptions of snprintf(3) return value
PR: 281160 Reviewed by: imp Pull Request: #1405 Closes: #1405
1 parent 2a678ff commit aae67a2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

usr.sbin/mfiutil/mfi_bbu.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,23 @@ mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
5050

5151
tmp = buf;
5252
if (d != 0) {
53-
tmp += snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
53+
int fmt_len;
54+
fmt_len = snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
55+
if (fmt_len < 0) {
56+
*buf = 0;
57+
return;
58+
}
59+
if ((size_t)fmt_len >= sz) {
60+
return;
61+
}
62+
tmp += fmt_len;
5463
sz -= tmp - buf;
5564
if (h != 0) {
56-
tmp += snprintf(tmp, sz, ", ");
65+
fmt_len = snprintf(tmp, sz, ", ");
66+
if (fmt_len < 0 || (size_t)fmt_len >= sz) {
67+
return;
68+
}
69+
tmp += fmt_len;
5770
sz -= 2;
5871
}
5972
}

0 commit comments

Comments
 (0)