对于与MAC解决方案QUOT;安全"替代"不安全" C / C ++标准库函数? [英] Mac solution for "safe" alternatives to "unsafe" C/C++ Standard Library functions?
问题描述
什么是最好的一站式商店安全在Mac上的C库解决方案?我使用引号的安全/不安全,因为有很多争论的某些标准库函数或其假定的改善方案带来的好处。
What's the best one-stop-shop "safe" C library solution on the Mac? I use quotes on "safe"/"unsafe" because there is much debate as to the benefits of certain Standard Library functions or their putatively improved alternatives.
许多传统的标准C库函数(例如, vfprintf
)被认为是不安全的,由于缓冲区溢出或其他潜在安全性问题。
Many traditional Standard C Library functions (e.g., vfprintf
) are considered to be unsafe due to the potential for buffer overflow or other security problems.
在Windows中,微软C / C ++编译器提供了_s功能(例如: , vfprintf_s
)作为标准库调用一个更安全的替代。这些功能都没有直接替代,因为他们有必要提供更多的安全信息(例如,缓冲区的长度)不同的签名。他们还提供其他功能,如格式字符串无效检测,不同的文件的安全性等。据我所知,这个实现不可用在Mac上。
On Windows, the Microsoft C/C++ compilers provide the "_s" functions (e.g., vfprintf_s
) as a safer alternative to the standard library calls. These functions are not drop-in replacements since they have the different signatures necessary to provide additional safety information (e.g., buffer length). They also provide other features such as invalid format string detection, different file security, etc. As far as I know, this implementation is not available on the Mac.
请问苹果公司(或第三方)提供与海湾合作委员会在OSX上使用了类似的话?
Does Apple (or a third party) provide anything similar for use with GCC on OSX?
在特定的,我正在寻找的安全实现的至少的以下功能:
In particular, I'm looking for "safe" implementations of at least the following functions:
的fopen vfprintf的sprintf vsprintf中strcpy的函数strncpy strcat的
的请注意:这个问题是关于在Mac。我不要求你对微软的实施意见(除非它是可在Mac上。)尽管其中的一些功能可能很容易对自己写的,不是所有人都是。我不问如何将这些写我自己。我不要求对如何使用STL类来完成这一提示。我不问如何关闭警告。我特别需要非常具体。我试图找出一个最佳实践的Mac的API,尽可能相似,同时增加安全传统的C库调用。当然,在Mac和Windows(和其他操作系统)工作的可移植的实现会更好。的
推荐答案
摘要:在Mac上,有几个API和编译器选项提供给C标准库函数更安全的替代。下面是其中一些与微软的安全的API 的:
SUMMARY: on Mac, there are several APIs and compiler options that provide safer alternatives to C Standard Library functions. Here are some of them compared with Microsoft's "safe" APIs:
C MSVC PROVIDERS MAC SOLUTION
---------------------------------------------------------------------------------
fopen fopen_s C none, assume fopen is safe
vfprintf vfprintf_s GCC GCC_WARN_TYPECHECK_CALLS_TO_PRINTF(1)
vsprintf vsprintf_s GCC, C99 GCC_WARN_TYPECHECK_CALLS_TO_PRINTF, vsnprintf(2)
sprintf sprintf_s GCC, C99 GCC_WARN_TYPECHECK_CALLS_TO_PRINTF, snprintf(3)
strncpy strncpy_s BSD strlcpy(4)
strcpy strcpy_s BSD strlcpy
strcat strcat_s BSD strlcat(5)
(1) GCC_WARN_TYPECHECK_CALLS_TO_PRINTF
是对应于GCC命令行选项的 -Wformat
。此选项产生的参数类型和静态格式字符串之间分歧的编译器警告。有多种其他选项来控制GCC的处理格式字符串。你甚至可以使用GCC的格式
功能属性使格式字符串检查你自己的功能。
(1) GCC_WARN_TYPECHECK_CALLS_TO_PRINTF
is an XCode configuration option which corresponds to the GCC command-line option -Wformat
. This option produces compiler warnings of disagreement between argument types and static format strings. There are a variety of other options to control GCC's treatment of format strings. You can even use GCC's format
function attribute to enable format string checking on your own functions.
(2)<一href=\"http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/snprintf.3.html\"><$c$c>vsnprintf$c$c>及(3)<一href=\"http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/snprintf.3.html\"><$c$c>snprintf$c$c>来自C99版本C标准库(海合会可在Mac上,但不是在MSVC在Windows上)。
(2) vsnprintf
and (3) snprintf
are from the C99 version of the C Standard Library (available in GCC on Mac but not in MSVC on Windows).
(4) strlcpy
以及(5)<一个href=\"http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/strlcat.3.html\"><$c$c>strlcat$c$c>是BSD库函数,可在Mac上。
(4) strlcpy
and (5) strlcat
are BSD library functions, available on Mac.
这篇关于对于与MAC解决方案QUOT;安全&QUOT;替代&QUOT;不安全&QUOT; C / C ++标准库函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!