0

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

  • `string stringArray[length];` -- This is not valid C++. C++ requires array sizes to be denoted by compile time constants, not variables. Use `std::vector stringArray(length);`. Then when you do that, look at this line: `stringArray[tmpTransnum] = ...`. How do you know that `tmpTransnum` is in bounds? Instead of that, do the following `stringArray.at(tmpTransnum) = ...`. If after changing to `vector`, you now get a `std::out_of_range` exception thrown, then that is one bug you had in your program you never knew about, and probably you were corrupting the heap all along. – PaulMcKenzie Mar 06 '19 at 04:44
  • 1
    [Probably a bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) at `while(!nodefd.eof())` as well. – user4581301 Mar 06 '19 at 04:56
  • Are you sure that `decryptedtext_len < 256`? Otherwise you would [smash thestack](https://en.wikipedia.org/wiki/Stack_buffer_overflow). – julians Mar 06 '19 at 08:11
  • Thx a lot. Conversion string array to vector made terminal clean. – actros_scm Mar 07 '19 at 04:31

1 Answers1

0

You can't define string stringArray[length] this way unless length is constexpr Two options

  1. Define a fixed length array as string stringArray[max] where max is declared as const or constexpr
  2. use std::vector
Soulimane Mammar
  • 1,099
  • 8
  • 17