sprintf_s和snprintf之间的区别是什么?

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

What is the difference between sprintf_s and snprintf?

问题

我在编写程序时遇到了这个问题,需要用到 sprintf 函数。

在某些情况下,使用 sprintf 函数可能导致内存溢出并构成安全风险。因此,可以使用 snprintfsprintf_s 函数,以避免这些风险。

但这两个函数的定义是相同的。那么,为什么存在这两个函数,而不是只有一个?

int sprintf_s(char *_DstBuf, size_t _DstSize, const char *_Format, ...);
int snprintf(char *__restrict__ __stream, size_t __n, const char *__restrict__ __format, ...);

起初,我以为 sprintf_s 函数是Windows库的独特之处,也以为 snprintf 函数是POSIX的独特之处。但实际上,这两个函数都包含在 <stdio.h> 库中。

英文:

I encountered this question while writing a program that requires the sprintf function.

In some cases, using the sprintf function can lead to memory overflow and pose a security risk. So, you can use the snprintf or sprintf_s functions, which are used to avoid these risks.

But the definitions of these two functions are the same. So, why do these two functions exist instead of just one?

int sprintf_s(char *_DstBuf, size_t _DstSize, const char *_Format, ...);
int snprintf(char *__restrict__ __stream, size_t __n, const char *__restrict__ __format, ...);

In the beginning, I thought that the sprintf_s function was unique to the Windows library, and I also thought that the snprintf function was unique to POSIX. But, in fact, both of these functions are included in the &lt;stdio.h&gt; library.

答案1

得分: 3

以下是已翻译好的内容:

存在snprintfsprintf_s函数之间的许多差异,特别是它们的返回值以及它们如何处理错误。

返回值(除了错误情况):

  • snprintf 返回的是如果忽略了“size”参数,将要写入缓冲区的字符数。

  • sprintf_s 返回实际写入的字符数。

额外检查

sprintf_s 函数还执行一些 snprintf 不执行的检查,包括以下情况之一的调用失败(返回零):

  1. 给定了 %n 格式说明符。
  2. %s 格式说明符对应的任何参数是空指针。
  3. 给定的“size”参数为零。
英文:

There are a number of differences between the snprintf and sprintf_s functions, notably in their return values and how they handle errors.

Return Values (barring errors):

  • snprintf returns the number of characters which would have been written to the buffer if the "size" argument were ignored.

  • sprintf_s returns the number of characters actually written.

Additional Checks:

The sprintf_s function also performs checks that snprintf does not, including. The call fails (and returns zero) if any of the following are true:

  1. The %n format specifier is given.
  2. Any of the arguments corresponding to a %s format specifier are null pointers.
  3. The given "size" argument is zero.

答案2

得分: 3

snprintfsprintf_s 的主要区别如下:

  1. 函数 snprintf 可在所有符合 ISO C 标准的平台上使用,而函数 sprintf_s 在大多数平台上并不存在。这是因为符合标准的平台不需要实现标准的 Annex K,而大多数平台选择不实现它。
  2. 函数 snprintf 如果字符串过大会默默截断字符串,而函数 sprintf_s 会调用当前安装的约束处理器函数。
  3. 函数 sprintf_s 会对函数参数进行额外的验证(例如检查是否为 NULL 指针),如果这些验证失败,会调用当前安装的约束处理器函数,而使用无效参数调用 snprintf 将会导致未定义行为(可能导致程序崩溃)。
英文:

The main differences between snprintf and sprintf_s are:

  1. The function snprintf is available on all ISO C compliant platforms, whereas the function sprintf_s does not exist on most platforms. This is because compliant platforms are not required to implement Annex K of the standard and most platforms have chosen not to implement it.
  2. The function snprintf will silently truncate the string if it is too large, whereas the function sprintf_s will call the currently installed contraint handler function.
  3. The function sprintf_s will perform additional validation of the function arguments (such as checking for a NULL pointer) and will call the currently installed constraint handler function if these validations fail, whereas calling snprintf with an invalid argument will invoke undefined behavior (i.e. possibly crash the program).

huangapple
  • 本文由 发表于 2023年4月17日 02:35:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029660.html
匿名

发表评论

匿名网友

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

确定