_snprintf()函数的负返回值

huangapple go评论76阅读模式
英文:

Negative return value of the _snprintf() function

问题

I am reviewing a legacy C code that calls _snprintf() function and then checks the status it returns to make amends if necessary.

As such, it checks for status == count being passed, which assumes that the count passed did not account for an extra NULL character, in which case the logic that I am reviewing is replacing the last character in the string with NULL.

Also, it checks for status == -1, and in that case creates an empty null-terminated string.

I was curious on which conditions the -1 value is returned, and found the following explanation in https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170:

If len > count, count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

You see, this article says just "negative value," without any distinction specifically for -1. This makes me suspicious if it is possible it can return other negative values (-2, -3, etc.) which seems the documentation implies. So I am curious as to what the specific negative return value means in this case, and most importantly, it looks to me that the existing code is not doing good enough work for testing for an abnormal situation, and instead of checking for status == -1, it should be checking for status < 0. So, is there a possibility to get back a negative number other than -1, and if so - what would it mean?

英文:

I am reviewing a legacy C code that calls _snprintf() function and then checks the status it returns to make amends if necessary.

As such, it checks for status == count being passed, which assumes that the count passed did not account for an extra NULL character, in which case the logic that I am reviewing is replacing the last character in the string with NULL.

Also, it checks for status == -1, and in that case creates an empty null-terminated string.

I was curious on which conditions the -1 value is returned, and found the following explanation in https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170:

> If len > count, count characters are stored in buffer, no
> null-terminator is appended, and a negative value is returned.

You see, this article says just "negative value", without any distinction specifically for -1. This makes me suspicious if it is possible it can return other negative values (-2, -3, etc.) which seems the documentation implies. So I am curious as to what the specific negative return value means in this case, and most importantly, it looks to me that the existing code is not doing good enough work for testing for abnormal situation, and instead of checking for status == -1, it should be checking for status &lt; 0. So, is there a possibility to get back a negative number other than -1, and if so - what would it mean?

答案1

得分: 2

> 在我看来,现有的代码在测试异常情况时做得不够好,而不是检查 status == -1,应该检查 status &lt; 0

正确。

> 那么,是否有可能得到一个除了 -1 以外的负数

有。

> 如果是这样 - 这意味着什么

这意味着发生了错误。

文档通常含糊其辞,以确保你检查特定的值。确切的值可能是实现细节,可能会更改。由于没有记录,你需要深入源代码或运行大量测试来找出,但这只对该函数的特定版本有效。

注意:除非明确记录要设置 errno,否则不要尝试使用 errno_snprintf 调用内部的许多事情都可能偶然设置 errno,从而给你一个错误的标志。

一个记录的情况,其中 errno 被设置并且有特定的返回值...

> 如果缓冲区是空指针且计数不为零,或者格式是空指针,则调用无效参数处理程序,如参数验证中所述。如果允许执行继续,这些函数返回 -1 并将 errno 设置为 EINVAL。

英文:

> it looks to me that the existing code is not doing good enough work for testing for abnormal situation, and instead of checking for status == -1, it should be checking for status &lt; 0

Correct.

> So, is there a possibility to get back a negative number other than -1

Yes.

> and if so - what would it mean?

It means there was an error.

Documentation is often vague like that to make sure you don't check for a specific value. The exact value is likely an implementation detail and can change. Since it is not documented, you'd have to dig into the source code or run a bunch of tests to find out, but that would only be valid for that specific version of the function.

Note: do not try to use errno unless it is specifically documented to set it. Lots of things inside the call to _snprintf can incidentally set errno giving you a false error flag.

There is one documented instance where errno is set and there is a specific return value...

> If buffer is a null pointer and count is nonzero, or if format is a null pointer, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.

huangapple
  • 本文由 发表于 2023年4月11日 06:20:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定