Search code examples
csocketspointersmemoryrealloc

Error in `./px': free(): invalid next size (normal)Aborted (core dumped)


Hello i was looking for a solution in many discussions ,unfortunantly without getting anything helpful.

I have a proxy server that take a request from one client and ask for a response from the server, the step I follow are:

1--> i made a request from the client to the proxy.

2--> the proxy connect to the server and send a response to the client (until here every thing works well).

3--> when i made a second request from the same client to the proxy,when the proxy try to connect a second time to the server this error occurs:

* Error in `./px': free(): invalid next size (normal): 0x0000000001941120 Aborted (core dumped) *

here is the method where the error occur:

char * readResponseFromServer(char *hostname,char *request,char *response){
int sock;
struct sockaddr_in serv_addr;
struct hostent * server;

sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0)
    printEror("socket opening error");

server=gethostbyname(hostname);

if (server == NULL)
      printEror ("unknown host");
printf("the server adress is  %s\n",server->h_name);

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
     (char *)&serv_addr.sin_addr.s_addr,
     server->h_length);
serv_addr.sin_port = htons(80);


if(connect(sock,(struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
     {
            printEror("connection error");

    }
int n1 = write(sock,request,strlen(request));
if (n1< 0) 
        printEror("ERROR writing to socket");
//reading response from server
int readed=0;
int bytesloaded=0;
int buf_size=512;
//we try to read 256 bytes each time
while( (readed = read(sock, response+ bytesloaded, buf_size)) > 0 )
    {   

    if(bytesloaded + readed >= buf_size)//the response buffer is full , so we allocate more memory
    {
        buf_size = buf_size + 256;  
        response = (char *)realloc(response, buf_size);

    }
    bytesloaded += readed;
    }

printf("the response is: %s",response);
close(sock);

}

and especially in this call:

    server=gethostbyname(hostname);

to sum up i get this error when i call the method above more than on time.

why i get this error?

Many thanks.

Solution:

I used the different suggestions to solve my problem, and I thank you once again.

the main problem was:

  1. a deallocation of memory problem with the free() method .
  2. a bad way to reallocate memory.

and the error was not in the Methode I posted but in another method despite that the program stops elsewhere, so it's one thing i have learned with this problem : the memory error could come from anywhere in your program.

so I had to review my program and I managed to find out where was the error memory.


Solution

    • Error in `./px': free(): invalid next size (normal): 0x0000000001941120 Aborted (core dumped) *

    In your code, assuming memory for char *request variable used in the following prototype:

    char * readResponseFromServer(char *hostname,char *request,char *response){
    

    is provided and freed from within the calling function...
    ...Then, if it is required that the buffer's memory be changed in the called function, the caller must pass the address of the variable, not the variable itself. In this way, realloc() can be used in the called function, while providing the caller ability to free the memory. Here is a snippet showing how this works using realloc():

    void growBuffer(char ** b, int *size);//note prototype include char **, not char *
    
    int main(void) 
    { 
        char *buf = {0};
        buf = calloc(20,1);
        strcpy(buf, "original string-");
        int len = strlen(buf)+1;
        printf("Original buf and length: %s - %d\n", buf, len);
        growBuffer(&buf, &len);//caller passes address of buf: (&buf) 
        printf("New buf and length: %s - %d\n", buf, len);
        free(buf);//caller can now safely free memory
    
        return 0;
    }
    
    
    
    void growBuffer(char **buffer, int *size)
    {
        const char newContent[]={"new content requires growing buffer"};
    
        int newlen = *size + strlen(newContent) + 1;
    
        char *buf = realloc((*buffer), newlen );
        if(buf)//always test return of [m][c][re]alloc before using
        {
            (*buffer) = buf; //assign new memory to buffer
            strcat((*buffer), newContent);//modify string
            *size = newlen;//pass back new len of string
        }
    }