错误:围绕变量'hexStr'堆栈在C++中损坏

问题描述:

我想使用AES算法。错误:围绕变量'hexStr'堆栈在C++中损坏

我用这个函数就可以了

void SubBytes(char *SArr[4][4]){ 
int r,c; 
char xlook[2];char ylook[2]; char hexStr[2]; char *Pad; 
int intxlook,intylook; 
char *temp; 
int subbyte; 
Pad = (char *)malloc (sizeof(char)); 
temp = (char *)malloc (sizeof(char)); 

for (r = 0; r < 4; r++) 
    for(c = 0; c < 4; c++){ 
     strcpy(temp,""); 
     strcpy(xlook,"");strcpy(ylook,""); 
     strcpy(temp,SArr[r][c]); 
     xlook[0] = temp[0]; xlook[1] = '\0'; 
     ylook[0] = temp[1];ylook[1] = '\0'; 
     intxlook = string_to_dec(xlook); 
     intylook = string_to_dec(ylook); 

    subbyte = SBox(intxlook,intylook); 
    itoa(subbyte,hexStr,16); 

    if (strlen(hexStr) < 2){ 
     strcpy(Pad,""); 
     Pad = Padding(0,1); 
     strcat(Pad,hexStr); 
     strcpy(SArr[r][c],Pad); 
    } 
    else 
     strcpy(SArr[r][c],hexStr); 

    } 

    //free(temp); free(Pad); 
}  //end 

,但我得到周围的变量 'hexStr' 错误

堆栈在C

你忘了空字符结束hexStr已损坏。

所以,当你做strcpy(SArr[r][c],hexStr);。它会尝试从hexStr复制到SArr[r][c],直到达到'\0',这可能在任何地方。

+0

谢谢,这是怎么回事? – 2012-07-17 08:04:24

+0

@ ar.gorgin将大小增加1,并将最后一个字符设置为“'\ 0''。 – 2012-07-17 08:13:01

temp具有用于单个字符足够的空间:

temp = (char *)malloc (sizeof(char)); 

含义,这将溢出缓冲区的端部和用于腐败可能的原因:

strcpy(temp,SArr[r][c]); 

由于这是C++,使用std::string而不是char*