Skip to content

Conversation

@Low-power
Copy link
Contributor

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

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

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 bsdimp added the changes-required Cannot land as is, change requested of submitter label Oct 4, 2024
@bsdimp
Copy link
Member

bsdimp commented Oct 4, 2024

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

@bsdimp bsdimp added the merged Closed commit that's been merged label Jun 13, 2025
@bsdimp
Copy link
Member

bsdimp commented Jun 13, 2025

Automated message from ghpr: Thank you for your submission. This PR has been merged to FreeBSD's branch. These changes will appear shortly on our GitHub mirror.

@bsdimp bsdimp closed this Jun 13, 2025
freebsd-git pushed a commit that referenced this pull request Jun 13, 2025
PR: 281160
Reviewed by: imp
Pull Request: #1405
Closes: #1405
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes-required Cannot land as is, change requested of submitter merged Closed commit that's been merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants