sprintf 何时以及为何会失败?

发布于 2024-09-03 08:13:13 字数 623 浏览 6 评论 0原文

我正在使用 swprintf 将字符串构建到缓冲区中(使用循环等)。

const int MaxStringLengthPerCharacter = 10 + 1;
wchar_t* pTmp = pBuffer;
for ( size_t i = 0; i < nNumPlayers ; ++i)
{
    const int nPlayerId = GetPlayer(i);
    const int nWritten = swprintf(pTmp, MaxStringLengthPerCharacter, TEXT("%d,"), nPlayerId);
    assert(nWritten >= 0 );
    pTmp += nWritten;
}

*pTaskPlayers = '\0';

如果在测试过程中断言从未命中,我能否确定它永远不会在实时代码中命中?也就是说,我是否需要检查 nWritten < 0 并处理这个问题,或者我可以放心地假设不会有问题吗?

什么情况下可以返回-1?文档或多或少只是说明“如果功能失败”。在一个地方我读到,如果它不能匹配参数(即可变参数的格式化字符串),它将失败,但这并不让我担心。

在这种情况下,我也不担心缓冲区溢出 - 我知道缓冲区足够大。

I'm using swprintf to build a string into a buffer (using a loop among other things).

const int MaxStringLengthPerCharacter = 10 + 1;
wchar_t* pTmp = pBuffer;
for ( size_t i = 0; i < nNumPlayers ; ++i)
{
    const int nPlayerId = GetPlayer(i);
    const int nWritten = swprintf(pTmp, MaxStringLengthPerCharacter, TEXT("%d,"), nPlayerId);
    assert(nWritten >= 0 );
    pTmp += nWritten;
}

*pTaskPlayers = '\0';

If during testing the assert never hits, can I be sure that it will never hit in live code? That is, do I need to check if nWritten < 0 and handle that, or can I safely assume that there won't be a problem?

Under which circumstances can it return -1? The documentation more or less just states "If the function fails". In one place I've read that it will fail if it can't match the arguments (i.e. the formatting string to the varargs) but that doesn't worry me.

I'm also not worried about buffer overrun in this case - I know the buffer is big enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

平生欢 2024-09-10 08:13:13

来自c99标准:

sprintf 函数返回写入数组的字符数,不包括终止空字符,如果发生编码错误,则返回负值。

这种情况通常仅发生在多字节和宽字符字符集函数中。

From the c99 standard:

The sprintf function returns the number of characters written in the array, not counting the terminating null character, or a negative value if an encoding error occurred.

This generally happens only with the multi-byte and wide character character set functions.

笑忘罢 2024-09-10 08:13:13

例如,它可能会因格式字符串错误而失败,这在您的情况下不会发生。

如果缓冲区不够大,也可以。

否则,没有理由失败。

It may fail with wrong format string, for example, which cannot happen in your case.

If buffer is not big enough, it may.

Otherwise, no reason for it to fail.

梦中的蝴蝶 2024-09-10 08:13:13

在 UNIX 中,它可能会失败:

 EILSEQ
       A wide-character code that does not  correspond  to  a
       valid character has been detected.

 EINVAL
       There are insufficient arguments.

EILSEQ 已经提到过。

当格式说明符与数据不匹配时,它也可能会失败,SIGSEGV - 例如使用 %s 格式说明符和 int、32 位示例:

int pdq=0xffffffff;
char tmp[32]={0x0};

sprintf(tmp, "%s", pdq);

In UNIX, it can fail:

 EILSEQ
       A wide-character code that does not  correspond  to  a
       valid character has been detected.

 EINVAL
       There are insufficient arguments.

EILSEQ has already been mentioned.

It may also fail, SIGSEGV, when the format specifier does not match the data - example using a %s format specifier with an int, 32 bit example:

int pdq=0xffffffff;
char tmp[32]={0x0};

sprintf(tmp, "%s", pdq);
反差帅 2024-09-10 08:13:13

我相信还有另一种情况 snprintf() 无法成功。 POSIX 或当前的 Linux 联机帮助页中似乎没有提及它。

成功完成后,snprintf() 函数应返回当 n 足够大时将写入 s 的字节数(不包括终止空字节)。

snprintf() 返回int。但输入字符串可能大于 INT_MAX

I believe there is another case where snprintf() cannot succeed. It does not appear to be mentioned in POSIX or the current Linux manpage.

Upon successful completion, the snprintf() function shall return the number of bytes that would be written to s had n been sufficiently large excluding the terminating null byte.

snprintf() returns int. But an input string could be larger than INT_MAX.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文