C/C++常见的问题分析讲解
程序员文章站
2022-06-27 22:37:43
总结使用C/C++遇到的问题及分析:
1.Run-Time Check Failure #2 - Stack around the variable 'hex'...
总结使用C/C++遇到的问题及分析:
1.Run-Time Check Failure #2 - Stack around the variable 'hex' was corrupted.
代码:
char bin[64] = "\x10\x5A\xFF"; char hex[64] = {0}; Bin2Hex(hex, sizeof(hex), bin, 3);
DWORD WINAPI Bin2Hex(LPSTR pHex, DWORD dwBufLen, LPCSTR pBin, DWORD dwLen) { DWORD dwNowLen = 0; if(NULL == pHex || NULL == pBin) { return 0; } for(DWORD i = 0; i < dwLen; i++) { dwNowLen += 2; sprintf_s(pHex + i * 2, dwBufLen, "%02X", (BYTE) *(pBin + i)); } return dwNowLen; }
调用sprintf_s函数,缓冲区大小不匹配所导致,改为:
sprintf_s(pHex + i * 2, dwBufLen - i * 2, "%02X", (BYTE) *(pBin + i));