英文:
What is the difference between sprintf_s and snprintf?
问题
我在编写程序时遇到了这个问题,需要用到 sprintf
函数。
在某些情况下,使用 sprintf
函数可能导致内存溢出并构成安全风险。因此,可以使用 snprintf
或 sprintf_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 <stdio.h>
library.
答案1
得分: 3
以下是已翻译好的内容:
存在snprintf
和sprintf_s
函数之间的许多差异,特别是它们的返回值以及它们如何处理错误。
返回值(除了错误情况):
-
snprintf
返回的是如果忽略了“size”参数,将要写入缓冲区的字符数。 -
sprintf_s
返回实际写入的字符数。
额外检查:
sprintf_s
函数还执行一些 snprintf
不执行的检查,包括以下情况之一的调用失败(返回零):
- 给定了
%n
格式说明符。 - 与
%s
格式说明符对应的任何参数是空指针。 - 给定的“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:
- The
%n
format specifier is given. - Any of the arguments corresponding to a
%s
format specifier are null pointers. - The given "size" argument is zero.
答案2
得分: 3
snprintf
和 sprintf_s
的主要区别如下:
- 函数
snprintf
可在所有符合 ISO C 标准的平台上使用,而函数sprintf_s
在大多数平台上并不存在。这是因为符合标准的平台不需要实现标准的 Annex K,而大多数平台选择不实现它。 - 函数
snprintf
如果字符串过大会默默截断字符串,而函数sprintf_s
会调用当前安装的约束处理器函数。 - 函数
sprintf_s
会对函数参数进行额外的验证(例如检查是否为NULL
指针),如果这些验证失败,会调用当前安装的约束处理器函数,而使用无效参数调用snprintf
将会导致未定义行为(可能导致程序崩溃)。
英文:
The main differences between snprintf
and sprintf_s
are:
- The function
snprintf
is available on all ISO C compliant platforms, whereas the functionsprintf_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. - The function
snprintf
will silently truncate the string if it is too large, whereas the functionsprintf_s
will call the currently installed contraint handler function. - The function
sprintf_s
will perform additional validation of the function arguments (such as checking for aNULL
pointer) and will call the currently installed constraint handler function if these validations fail, whereas callingsnprintf
with an invalid argument will invoke undefined behavior (i.e. possibly crash the program).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论