-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mfiutil: Fix unsafe assumptions of snprintf(3) return value in function mfi_autolearn_period
#1405
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,13 +50,24 @@ | |
|
||
tmp = buf; | ||
if (d != 0) { | ||
tmp += snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s"); | ||
int fmt_len; | ||
fmt_len = snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s"); | ||
if (fmt_len < 0) { | ||
*buf = 0; | ||
return; | ||
} | ||
if ((size_t)fmt_len >= sz) return; | ||
Check failure on line 59 in usr.sbin/mfiutil/mfi_bbu.c GitHub Actions / Style Checker
|
||
tmp += fmt_len; | ||
sz -= tmp - buf; | ||
if (h != 0) { | ||
tmp += snprintf(tmp, sz, ", "); | ||
fmt_len = snprintf(tmp, sz, ", "); | ||
if (fmt_len < 0 || (size_t)fmt_len >= sz) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or maybe just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree'd |
||
return; | ||
} | ||
tmp += fmt_len; | ||
sz -= 2; | ||
} | ||
} | ||
Check warning on line 70 in usr.sbin/mfiutil/mfi_bbu.c GitHub Actions / Style Checker
|
||
if (h != 0) | ||
snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s"); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proposed change won't hurt but I don't think
snprintf
can return a negative number here, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns the number of characters that would be written if there was an unlimited buffer. It might be > sz.
However, it can return < 0 if there's an error in formatting (well, the man page just says "an error") so we return at least an empty buffer.