c ++ ecliipse free():函数结尾处的指针无效

时间:2019-03-06 03:46:46

标签: c++

我正在使用Eclipse和OpenSSL编写程序。 我已经测试了我的代码并收到了free():无效的指针错误。 但是我没有在代码中使用 new delete ,在函数 aes_decrypt()的结尾出现了自由错误(请参见打击) 它可以正常工作8次(称为9次),但是接下来,正如我所说的那样发生了错误。 我错过了什么吗?如果是这样,我该如何解决该错误? 多谢。

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;
}}

函数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.

在终端

some hash...

一些哈希...

gethash()一些哈希...

getmerkle()一些哈希...

一些哈希...

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

tmp2��ǂ�sS=e��R<��O�*���Z[�6Zb�iQ�&u_�b�T���oT�p�MB。 �Xxъ5�X

tmpChar:��ǂsS=e��R<��O�*���Z[�6Zb�iQ�&u_�b܈�T���oP�MB ����Xxъ5�X长度:80

结果 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长度:32

free():无效的指针

1 个答案:

答案 0 :(得分:0)

除非string stringArray[length]length,否则您不能这样定义constexpr 两种选择

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