`snprintf_s`假的安全感 [英] False sense of security with `snprintf_s`
问题描述
MSVC的安全 sprintf
函数具有知道目标缓冲区大小的模板版本。但是,这段代码在字节 ...
MSVC's "secure" sprintf
funcions have a template version that 'knows' the size of the target buffer. However, this code happily paints 567890 over the stack after the end of bytes
...
char bytes[5];
_snprintf_s( bytes, _TRUNCATE, "%s", "1234567890" );
任何想法我做错了什么,还是这是已知的错误?
Any idea what I do wrong, or is this a known bug?
(我在VS2005中工作 - 在2008或2010年没有测试)
(I'm working in VS2005 - didn't test in 2008 or 2010)
推荐答案
它似乎是 Visual C ++ 2005中的一个错误(我无法访问该链接; Google也有缓存)。
It does appear to be a bug in Visual C++ 2005 (I'm having trouble getting to that link; Google also has it cached).
我能够在Visual C ++ 2005中重现问题。在Visual C ++ 2008和2010中,字符串被正确截断( bytes
包含 1234\0
)和 -1
。
I was able to reproduce the problem in Visual C++ 2005. In Visual C++ 2008 and 2010, the string is correctly truncated (bytes
contains 1234\0
) and -1
is returned as expected.
这篇关于`snprintf_s`假的安全感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!