简体   繁体   English

为什么使用strsafe.h StringCch函数会导致C6386缓冲区溢出错误?

[英]Why error C6386 buffer overrun with strsafe.h StringCch functions?

So I ran an Analyze in VS 2017 with my C++ code. 因此,我使用C ++代码在VS 2017中进行了分析。 It gives me a buffer overrun with the following: 它为我提供了以下缓冲区溢出:

TCHAR *sTemp = new TCHAR[5]();
if (sTemp)
    StringCchCopy(sTemp, 5, L"0123456789");

When I step through the code, sTemp is "0123", with the 4th position of course being \\0. 当我单步执行代码时,sTemp为“ 0123”,第4个位置当然为\\ 0。

When I run Analyze on the code, I get the C6386 error: 在代码上运行Analyze时,出现C6386错误:

Warning C6386   Buffer overrun while writing to 'sTemp':  the writable size is 'unsigned int' bytes, but '10' bytes might be written.

Why? 为什么? I have also tried changing the array to 10 and the StringCchCopy to 5 and still the same error. 我也尝试将数组更改为10,将StringCchCopy更改为5,并且仍然是相同的错误。

The warning refers to the fact, that the source string will not ever fit inside the destination. 该警告指的是源字符串永远无法放入目标的事实。 The source string has a length of 10, the destination a size of 5 code units. 源字符串的长度为10,目标字符串的长度为5个代码单元。 It's not relevant at all, that the static analyzer cannot determine the size of the dynamically allocated destination array. 静态分析器无法确定动态分配的目标数组的大小完全无关紧要。

If it were, and it would discover a mismatch between the actual size and the size you claimed, it would raise an error, not a warning. 如果是这样,并且会发现实际大小与您声明的大小不匹配,则会引发错误,而不是警告。

The docs for StringCchCopy say that the second parameter must be the size of the destination buffer and that the destination buffer must be big enough to hold the source string. StringCchCopy的文档说第二个参数必须是目标缓冲区的大小, 并且目标缓冲区必须足够大以容纳源字符串。 You're not checking the return code from the function but I suspect it will be STRSAFE_E_INSUFFICIENT_BUFFER, which means "The copy operation failed due to insufficient buffer space. The destination buffer contains a truncated, null-terminated version of the intended result. In situations where truncation is acceptable, this may not necessarily be seen as a failure condition." 您没有检查该函数的返回代码,但我怀疑它将是STRSAFE_E_INSUFFICIENT_BUFFER,这意味着“复制操作由于缓冲区空间不足而失败。目标缓冲区包含预期结果的截断的,以空值终止的版本。在某些情况下如果可以接受截断,则不一定将其视为失败情况。”

https://docs.microsoft.com/en-us/windows/win32/api/strsafe/nf-strsafe-stringcchcopyw https://docs.microsoft.com/zh-cn/windows/win32/api/strsafe/nf-strsafe-stringcchcopyw

I guess you're happy with, and expecting, the truncation, but the static analysis tool is seeing that your source string is longer than your destination buffer and triggering the warning. 我猜您对截断感到满意并期待,但是静态分析工具发现您的源字符串比目标缓冲区长,并触发了警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 避免“缓冲区溢出” C6386警告 - Avoiding 'Buffer Overrun' C6386 warning VS2019:[C6386] 缓冲区溢出而到 - VS2019: [C6386] Buffer Overrun while to 为什么我在写入“ptr”时收到“C6386”缓冲区溢出警告? - Why am I getting warning 'C6386' Buffer overrun while writing to 'ptr'? 为什么在Visual Studio 2012的代码分析中,此代码为什么发出缓冲区溢出警告(C6385 / C6386)? - Why does this code emit buffer overrun warnings(C6385/C6386) in code analysis on Visual Studio 2012? 错误:C6386:写入“newArr”时缓冲区溢出:可写大小为“int current_size*1”字节,但可能写入“2”字节 - Error: C6386: Buffer overrun while writing to 'newArr': the writable size is 'int current_size*1' bytes, but '2' bytes might be written __m256 阵列上的 Visual Studio 2019 C6385 / C6386(缓冲区溢出警告) - Visual Studio 2019 C6385 / C6386 (buffer overrun warning) on __m256 array VS2015:[C6386] 写入时缓冲区溢出(即使对于相同的索引值) - VS2015: [C6386] Buffer Overrun while writing (even for same index value) Visual Studio 2015 代码分析 C6386 警告缓冲区溢出 - Visual Studio 2015 Code Analysis C6386 warns of buffer overrun 使用动态分配的 arrays 导致来自代码分析的 C6386 缓冲区溢出警告 - Using dynamically-allocated arrays causes C6386 Buffer Overrun warning from Code Analysis 在tchar.h之后需要包含strsafe.h错误 - Need to include strsafe.h after tchar.h Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM