_snprintf_s instokes [peceraiton] .exe触发了断点
_snprintf_s invokes [applicaiton].exe has triggered a breakpoint
_snprintf工作正常。但是为什么_snprintf_s触发了断点。是否有任何错误/或我缺少非常重要的东西。
std::string hash_sha256_sa(const std::string source)
{
const int HASH_STRING = 64;
const int HASH_RAW = 32;
unsigned char _hash[HASH_RAW];
memset(_hash, 0, HASH_RAW);
sha256(_hash, (unsigned char*)&source[0], (unsigned long)source.length());
std::string str(HASH_STRING, 0);
for (int i = 0; i < HASH_RAW; ++i)
{
//_snprintf_s(&str[i * 2], HASH_STRING, _TRUNCATE, "%02X", _hash[i]); //Failed
//_snprintf(&str[i * 2], HASH_STRING,"%02X", _hash[i]); //Works
}
return str;
}
第二个变体提供相同的结果。_snprintf工作正常。但是_snprintf_s结果以下错误
运行时检查失败#2-围绕变量"缓冲区"堆叠为 损坏。
std::string hash_sha256_sa(const std::string source)
{
const int HASH_STRING = 64;
const int HASH_RAW = 32;
unsigned char _hash[HASH_RAW];
memset(_hash, 0, HASH_RAW);
sha256(_hash, (unsigned char*)&source[0], (unsigned long)source.length());
char buffer[HASH_STRING + 1];
for (int i = 0; i < HASH_RAW; ++i)
{
_snprintf_s(&buffer[i * 2], _countof(buffer), _TRUNCATE, "%02X", _hash[i]);
//_snprintf(&buffer[i * 2], _countof(buffer), "%02X", _hash[i]);
}
std::string str(buffer);
return str;
}
_snprintf_s
是该功能的Microsoft提供的"安全"版本。如果是触发断点,那么您在函数调用中做错了什么,并且该错误旨在帮助您检测该错误。_snprintf
是不安全的,偶然起作用(因为它实际上依赖于未定义的行为)。
在这种情况下,显然您将错误的长度传递给缓冲区(第二个参数到函数)。尽管str
确实确实具有HASH_STRING
的总长度,但您正在索引到缓冲区(str[i * 2]
)并返回指针到该子buffer。显然,子缓冲器的长度不能与总缓冲区相同!
您不是将buffer
发送到_snprintf_s
,而是发送&buffer[i * 2]
。每次增量,&buffer[i * 2]
的可用大小都会降低2。因此,更改为第二个参数,如下:
_snprintf_s(&buffer[i * 2], _countof(buffer) - i * 2, _TRUNCATE, "%02X", _hash[i]);
相关文章:
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 终端不会为C++文件创建.exe文件吗
- 在Windows中以.exe的形式运行c++
- 无法删除指针,已触发断点
- Visual Studio mkl_link_tool.exe链接错误
- 条件断点在不应该触发时触发
- PrintWindow函数在notepad.exe中出错
- VSCode C++ 编译的exe感染了Win32:TrojanX-gen[Trj]
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 项目名称.exe 已触发断点
- ".exe"触发了断点
- Delete[] 运算符使 *.exe 触发断点和 wntdll.pdb未加载以显示
- 测试项目.exe触发了断点
- _snprintf_s instokes [peceraiton] .exe触发了断点
- C++控制台应用程序 1.exe 已触发断点
- 项目.exe在系统("pause")之后触发了断点
- MyTest1.exe 在 C++ / MFC 程序中触发了删除命令的断点
- 从双重和错误中丢失数据:Windows 在 exe 中触发了断点.这可能是由于堆损坏
- OpenCV-FindContours-Projekt.exe已触发断点
- Windows在myprogram.exe中触发了一个断点