I'm making a program with Eclipse and OpenSSL. I've test my code and receive free():invalid pointer error. But I didn't use new and delete in my code, free error was occurred at end of function, aes_decrypt()(see blow) It works well for 8 times(called 9 times) but at next, error occurred as I said. Did I missed something? If so, how can I solve this error? thx a lot.
main()
{
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;}
function 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 == "\0")
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 !="\0"){
string substr;
while(true){
getline(nodefd,substr);
if(substr == "\0")
break;
tmp +='\0';
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;
}}
function 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] = '\0';
return (const char*)decrypt_result;}//free(): invalid pointer error occurred program terminate.
at terminal
some hash...
some hash...
gethash() some hash...
getmerkle() some hash...
some hash...
loading 2019-03-04 13:46:32.txt open node 2019-03-04 13:46:32 length 9
tmp2 ��ǂ�sS=e��R<�� O�*���Z[�6�Zb�iQ�&u_���b܈�T���`oT�p�MB����Xxъ5�X
tmpChar : ��ǂ�sS=e��R<�� O�*���Z[�6�Zb�iQ�&u_���b܈�T���`oT�p�MB����Xxъ5�X length : 80
result 2019_02_26T08_00_06_35_2_Connect_debian_sys_maint@localhost_onusing_Socket
...
tmp2 �j^�I�C-�32��n�vݚ�¼�<��f�J�����s,O�քգ������Ŏ�#N=�4��<�Cո�D�yG�'�2
tmpChar : �j^�I�C-�32��n�vݚ�¼�<��f�J�����s,O�քգ������Ŏ�#N=�4��<�Cո�D
�yG�'�2 length : 32
free(): invalid pointer