Run-Time Check Failure #2 - Stack around the variable 'hex' was corrupted.

C++程序调度时发现一段代码退出时会有“Run-Time Check Failure #2 - Stack around the variable 'hex' was corrupted.”报错g。在网上找了一些信息,大部分是说由于堆栈溢出导致出错,可是我的代码看起来并没有溢出的情况。有看到网上的暂时规避思路,把hex[2]改为hex[3]就无此问题。原因暂不明确。

string Encrypt(string plaintext) {
ciphertext = "";
char plainchar[12];
char cipherchar[12] = "";
for (unsigned int i = 0; i < plaintext.size(); i += 8) { //把明文按每8个字节分割
strcpy(plainchar, plaintext.substr(i, 8).c_str());
memset(cipherchar, 0, sizeof(cipherchar)); //把cipherchar每一位赋值0
Run(cipherchar, plainchar, true); //获取对应密文
unsigned char buf[8] = {0};
memcpy(buf, cipherchar, 8);
for (int j = 0; j < 8; j++)
{
char hex[2] = {0};//后来改为hex[3]
IntToHex(buf[j], hex);

ciphertext += hex;
}
}

return ciphertext;
}

void IntToHex(int i, char buf[2])//后来改为buf[3]
{
sprintf((char *)buf, "%02x", i);
}

posted @ 2019-07-15 16:00  草秋千  阅读(1152)  评论(0编辑  收藏  举报