Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Low-power
Copy link
Contributor

@Low-power Low-power commented Sep 3, 2024

sz -= tmp - buf;
if (h != 0) {
tmp += snprintf(tmp, sz, ", ");
fmt_len = snprintf(tmp, sz, ", ");
if (fmt_len < 0 || (size_t)fmt_len >= sz) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe just strlcat()? I don't see a need to use snprintf here...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree'd

@@ -50,10 +50,21 @@ mfi_autolearn_period(uint32_t period, char *buf, size_t sz)

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");
Copy link
Member

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?

Copy link
Member

@bsdimp bsdimp Sep 21, 2024

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.

@Low-power
Copy link
Contributor Author

The proposed change won't hurt but I don't think snprintf can return a negative number here, no?

I don't think so either. Just pedantic, since I need to cast fmt_len into size_t to compare it with sz; better to make sure it isn't negative first.

On other hand, the man page did mentioned a few error conditions for this function. It is theoretically possible for snprintf(3) to fail due to an internal buffer allocation failure in the C library.

@Low-power
Copy link
Contributor Author

Low-power commented Sep 6, 2024

or maybe just strlcat()? I don't see a need to use snprintf here...

strlcpy(3) should be used. Then the checking for negative number can be eliminated because it returns size_t.

I didn't use these functions in my fork because they are not portable.

@bsdimp bsdimp self-assigned this Oct 4, 2024
@bsdimp
Copy link
Member

bsdimp commented Oct 4, 2024

looks like negative return value from snprintf isn't handled still...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants