C++ ecliipse free():函数末尾的指针无效

c++ ecliipse free(): invalid pointer at end of function

本文关键字:指针 无效 函数 ecliipse free C++      更新时间:2023-10-16

我正在用Eclipse和OpenSSL制作一个程序。 我已经测试了我的代码并收到免费():无效的指针错误。 但是我没有在我的代码中使用newdelete,函数结束时发生了免费错误,aes_decrypt()(见吹) 它工作正常 8 次(调用 9 次),但接下来,正如我所说发生了错误。 我错过了什么吗?如果是这样,我该如何解决此错误? 非常感谢。

主()

{
in.open("/tmp/log.txt");
int size=11;
string trans[11];
string tmp;
for(int i=0; i<size;){
getline(in,tmp);
if(tmp[0]=='2'&&tmp[1]=='0'){
tmp=aes_MakeDecryptable(tmp);//Dont care about this function.
trans[i]=tmp;
i++;
}
}
Block myblock(0,trans,4);
myblock.BlockGen();
mychain.AddBlock(myblock);
cout<<"gethash() "<<mychain._GetLastBlock().GetHash()<<endl;
cout<<"getmerkle() "<<mychain._GetLastBlock().GetMerkle()<<endl;
cout<<myblock.CMerkle()<<endl;
cout<<Loadandcheck("2019-03-04 13:46:32")<<endl;//open and check stability 2019-03-04 13:46:32.txt
sleep(5);
cout<<"gethash() "<<myblock.GetHash()<<endl;//sha512 hash return
cout<<"getmerkle() "<<myblock.GetMerkle()<<endl;//merkle hash return
cout<<myblock.CMerkle()<<endl;//merkle hash return
return 0;}

函数 Loadandcheck()

string Loadandcheck(string node){
string result;
ifstream nodefd;
ifstream lengfd;
string tmp;
int length=0;
unsigned char tmpChar[256];
int tmpTransnum=0;
nodefd.open(node+".txt");
lengfd.open(node+"_leng.txt");
cout<<"loading "<<node+".txt"<<endl;
if(!nodefd.is_open() || !lengfd.is_open()){
cout<<"couldn't find node"<<endl;
return "-1";
}
else{
cout<<"open node ";
getline(nodefd, tmp);
cout<<node<<endl;
for(; !nodefd.eof();){
getline(nodefd,tmp);
if(tmp == "")
length++;// get node number
}
length -=2;
cout<<"length "<<length<<endl;
string stringArray[length];
length =0;
//nodefd.close();
//nodefd.open(node+".txt");
nodefd.seekg(0, ios::beg);
getline(nodefd,tmp);
lengfd>>length;//length for aes decrypt
while(!nodefd.eof()){
if(tmp !=""){
string substr;
while(true){
getline(nodefd,substr);
if(substr == "")
break;
tmp +='';
tmp +=substr;
}
}
cout<<"tmp2 "<<tmp<<endl;
aes_StringToCharArr(tmp,tmpChar);
cout<<"tmpChar : "<<tmpChar<<" length : "<<length<<endl;
stringArray[tmpTransnum]=aes_decrypt(tmpChar,length,iv,key);//at here, free() error occurred after 9 times called, Before? fine
cout<<"result "<<stringArray[tmpTransnum]<<endl;
tmpTransnum++;
if(nodefd.eof() || lengfd.eof())
break;
getline(nodefd,tmp);
lengfd>>length;
}
cout<<"done"<<endl;
tmpTransnum=0;
nodefd.close();
lengfd.close();
cout<<endl<<"currently working at ";
for(int i=0; i<length;i++)
cout<<stringArray[i]<<endl;
cout<<endl;
result=LoadnMerkle(0,tmpTransnum,stringArray);
return result;
}}

函数 aes_decrypt()

string aes_decrypt(unsigned char* ciphertext,int ciphertext_len,unsigned char *iv, unsigned char* key){
unsigned char decrypt_result[256];
int decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,decrypt_result);
/* Add a NULL terminator. We are expecting printable text */
decrypt_result[decryptedtext_len] = '';
return (const char*)decrypt_result;}//free(): invalid pointer error occurred program terminate.

在终端

some hash...

一些哈希...

gethash() some hash...

getmerkle() some hash...

一些哈希...

加载时间:2019-03-04 13:46:32.txt 开放节点 2019-03-04 13:46:32 长度 9

tmp2 ǂ sS=e R

tmpChar : ǂ sS=e R

结果 2019_02_26T08_00_06_35_2_Connect_debian_sys_maint@localhost_onusing_Socket

tmp2 j^ I C- 32 n v� 1/4�yG�'�2 tmpChar : �j^�I�C-�32��n�vݚ�¼�<��f�J�����s,O�քգ������Ŏ�#N=�4��<�Cո�D=" yG=" 2 长度 : 32</p">

free():无效指针

除非lengthconstexpr,否则您无法以这种方式定义string stringArray[length]两个选项

  1. 将固定长度数组定义为string stringArray[max]其中 max 声明为 const 或 constexpr
  2. 使用 std::vector