jeudi 28 juin 2018

send error : bad address

I am trying to send some data (cryptographic material) throught a TCP socket in my localhost. So i send first the size of the datas, and it works well. Then i send the real data. It starts well, my server getting some of it but after some time, it crash giving me

Send error: Bad address

*** Error in `/home/stagiaire031/Bureau/HElib-master/src/serveur': free():

invalid next size (normal): 0x0000000000882cc0 ***

i've tried to valgrind it to get some information, but i only got this :

--7732-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting --7732-- si_code=128; Faulting address: 0x0; sp: 0x803426e30

valgrind: the 'impossible' happened: Killed by fatal signal

host stacktrace: ==7732== at 0x38091C12: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

==7732== by 0x38050E84: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

==7732== by 0x380510A9: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

==7732== by 0x380D4F7B: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

==7732== by 0x380E3946: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

sched status: running_tid=1

I'm not really used to valgrind, and the only things i found about this error is this, but i don't find somwhere in my code where i could have write before or after my buffer.

The other weird things is that sometimes my server get more data than other times before crashing.

here is the relevant code for the server :

char *buffer = 0;
buffer = new char[4096];
stringstream s2;
s2.str();
long n = recv(newsockfd, buffer, 50, 0);
cout << buffer << endl;
double size = stod(buffer, nullptr);
cout << "i receive this size of message : " << n << endl;
n = 0;
int i = 0;
cout << "we wait the message" << endl;
while (i < size)
{
    n = read(newsockfd, buffer, size);
    if (n < 0)
    {
        cout << "we got a receive error" << n << endl;
        perror("Send error");
    }
    else
    {
        cout << "concatenate strings" << endl;
        s2 << *buffer;
        cout << "we receive " << n << " and all : " << i << endl;
        i += n;
    }
    bzero(buffer, 4096);
}

and here is the one for the client :

n = send(sockfd, s2.str().c_str(), 10, 0);
cout << "context size " << s1.str().length() << endl;
n = 0;
int i = 0;
while (i < s1.str().length())
{
    n = send(sockfd, s1.str().c_str(), s1.str().length() - i, 0);
    if (n < 0)
    {
        perror("Send error");
    }
    else
    {
        cout << "end of a turn... we send " << n << endl;
        i += n;
        s1.str() += n;
    }
}

i've check out some post about error with socket, like this this ,this or this. They helped me before but seems like not enought for my current problem.

Thanks everyone for any help!

Aucun commentaire:

Enregistrer un commentaire