c++ - 为什么错误 C6386 缓冲区溢出 strsafe.h StringCch 函数?
问题描述
所以我用我的 C++ 代码在 VS 2017 中运行了分析。它给了我一个缓冲区溢出,如下所示:
TCHAR *sTemp = new TCHAR[5]();
if (sTemp)
StringCchCopy(sTemp, 5, L"0123456789");
当我单步执行代码时,sTemp 是“0123”,第 4 位当然是 \0。
当我对代码运行分析时,我收到 C6386 错误:
Warning C6386 Buffer overrun while writing to 'sTemp': the writable size is 'unsigned int' bytes, but '10' bytes might be written.
为什么?我还尝试将数组更改为 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. 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.
推荐阅读
- blockchain - 如何在 RIDE 中从 tx.sender 获取字符串地址?
- python - 获取带有空格的 pandas df 列的 len()
- javascript - 无法将下一个 js 部署到 azure
- ios - 如何在 UIView 中定位和动画子视图?
- flutter - 将地图项读入类成员
- machine-learning - CNN网络,继续使用conv2d,不使用maxpool,看keras书的一个问题
- python - 将一个 dask 数据帧包写入磁盘(使用 dask 和 featuretools 生成 200 万个特征)
- vuejs2 - 带有笑话的Vue Cli单元测试如何运行单个测试
- django - 从视图中保存查询时出现不完整错误
- json - 为什么隐式读取找不到我的对象类型scala