2013-02-08 3 views
0

Я пытаюсь протестировать клиентскую программу, чтобы убедиться, что она правильно отправляет и получает сообщения с сервера. Когда я запускаю программу, я получаю эту странную ошибку от malloc. Любая помощь будет большой. Я слишком долго смотрел на этот код. Заранее спасибо. Здесь ошибка:Получение ошибки malloc при запуске простой клиентской программы

// Первый тест проходит нормально

TEST 1 : 
Connected to server 
-> Socket open success 
-> Request sent success 
-> Response received success 
Testing print: 
Response received: <replyLoadAvg>0.228027:0.250000:0.280273</replyLoadAvg> 

LoadAvg for 1min :: 0.228027 
LoadAvg for 5min :: 0.250000 
LoadAvg for 15min :: 0.280273 
Real response -> <replyLoadAvg>0.228027:0.250000:0.280273</replyLoadAvg> 

// Тогда я получаю эту ошибку по какой-то причине

clientc(17781) malloc: ********* error for object 0x7fd048404318: incorrect checksum for   
freed object - object was probably modified after being freed. 
********* set a breakpoint in malloc_error_break to debug 
Abort trap: 6 

здесь код клиента:

// prototype 

char* verifyString(char*); 

void printLoadAvg(char*); 

int receiveResponse(int, char **); 

int main(int argc, char *argv[]) 
{ 
    int sockfd; 
    char *name; 
    char *port; 
    struct sockaddr_in * dest; 

    if(argc != 3) 
    { 
     printf("Not enough arguments -- Exiting program\n"); 
     return 0; 
    } 

    name = argv[1]; 
    port = argv[2]; 

    char * received; 
    char send[256]; 

    char testMessages[5][256] = {"<loadavg/>", "<echo>Hello World!</echo>", "<echo>      </echo>", "", "<echo>Bye Bye World...<echo>"}; 
    char expectedResponses[5][256] = {"", "<reply>Hello World!</reply>", "<reply></reply>","<error>unknown format</error>", "<error>unknown format</error>"}; 

    int i; 
    srand(time(NULL)); 

    for(i = 0; i < 5; i++) 
    { 
     printf("\nTEST %i", i+1); 
     printf(((rand() % 2) ? ":\n" : " :\n")); 
     sprintf(send, "%s", testMessages[i]); 
     sockfd = createSocket(name, atoi(port), &dest); 
     if(sockfd < 0){ 
      printf(" xx Open socket failed\n"); 
     } 
     else{ 
      printf(" -> Socket open success\n"); 
      if(sendRequest(sockfd, send, dest) < 0){ 
       printf(" xx Send failed\n"); 
      } 
      else{ 
       printf(" -> Request sent success\n"); 
       if(receiveResponse(sockfd, &received) < 0){ 
        printf(" xx Receive failed\n"); 
       } 
       else{ 
        printf(" -> Response received success\n"); 
        if(i!=0 && strcmp(received, expectedResponses[i]) != 0){ 
         printf("Incorrect message\n"); 
         printf("Expected: %s\n", expectedResponses[i]); 
         printf("Received: %s\n", received); 
        } 
        else if(i!=0){ 
         printf("Test %i. passed successfully!\n", i+1); 
        } 
        else{ 
         printf("Testing print:\n"); 
         printResponse(received); 
         fprintf(stdout, "Real response -> %s\n", received); 
        } 
       } 
      } 
     } 

     free(received); 
     free(dest); 

     if(closeSocket(sockfd)==0){ 
      printf(" -> Socket closed success\n"); 
     } 
     else{ 
      printf(" XX Close failed\n"); 
     } 
    } 

    return 0; 
} 


int createSocket(char* hostname, int port, struct sockaddr_in** dest) 
{ 
    // fill in the socket address struct 
    (*dest) = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)); 
    struct hostent *hostptr; 
    hostptr = gethostbyname(hostname); 
    int socketfd; 

    memset((void *) (*dest), 0, (size_t)sizeof(struct sockaddr_in)); 
    (*dest)->sin_family = (short)(AF_INET); 
    memcpy((void*)& (*dest)->sin_addr, (void *) hostptr->h_addr, hostptr->h_length); 
    (*dest)->sin_port = htons((u_short)port); 

    // now get the file descriptor for the socket 
    if((socketfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { 
      return -1; 
    } 

    // connect to the server at the location displayed by TCPserver.c 
    if(connect(socketfd, (struct sockaddr*)(*dest), sizeof(struct sockaddr)) < 0) { 
     perror("Error: call to connect()\n"); 
     return -1; 
    } 

     printf("Connected to server\n"); 

    return socketfd; 
} 


int sendRequest(int sock, char* request, struct sockaddr_in* dest) 
{ 
    // send a request through the socket to the server. 
    if(write(sock, request, BUFFERSIZE) < 0) 
    { 
     return -1; 
    } 

    return 0; 
} 

int receiveResponse(int sock, char ** response) 
{ 
    // dynamically allocate memory to be used in this function 
    (*response) = malloc(sizeof(char)); 

    // wait for a response from the server 
    if(read(sock, (*response), BUFFERSIZE) < 0) 
    { 
     return -1; 
    } 

    return 0; 
} 

int closeSocket(int sock) 
{ 
    int error = 0; 

if(close(sock)) 
    { 
     perror("Error: Failed to close socket\n"); 
     error = -1; 
    } 

    return error; 

} 

void printResponse(char* response) 
{ 
    char* buffer = "<replyloadavg>"; 
    int i, isLoadAvg = 0; 

    // verify the string is in the correct format by calling the verifty string function 
    // then print the string to the screen. 
    printf("Response received: %s\n", verifyString(response)); 

     for(i = 0; i < 14; i++) 
     { 
      if(response[i] == buffer[i]) 
       isLoadAvg = 0; 
      else 
      { 
       isLoadAvg = 1; 
       i = 15; 
      } 
     } 

     if (isLoadAvg) 
     { 
      printLoadAvg(response); 
     } 
    } 

char* verifyString(char* string) 
{ 
    int i, count = 0; 

    // step through the array until you come to the last '>' (the end of the string) 
    // then insert the NULL terminator at the end of the string. 
    for(i = 0; i < BUFFERSIZE; i++) 
    { 
     if(string[i] == '>') 
      count++; 
     if(count == 2) 
     { 
      string[i + 1] = '\0'; 
      i = BUFFERSIZE; 
     } 
    } 

    return string; 
} 

void printLoadAvg(char* string) 
{ 
    int i = 0, j; 

    // these three arrays hold the newly formated strings, the ones that are easier to read. 
    char s1[BUFFERSIZE], s2[BUFFERSIZE], s3[BUFFERSIZE]; 

    // step through the string until you reach the end of the string 
    while(string[i] != '>') 
    { 
     i++; 
    } 

    i++; 

    // now insert a colon and then save the contents of the string to the appropriate s array. 
    for(j = 0; string[i] != ':'; j++, i++) 
    { 
     s1[j] = string[i]; 
    } 

    // be sure to set the null terminator at the end. 
    s1[j] = '\0'; 
    i++; 

    for(j = 0; string[i] != ':'; j++, i++) 
    { 
     s2[j] = string[i]; 
    } 

    // be sure to set the null terminator at the end. 
    s2[j] = '\0'; 
    i++; 

    // now move to the beginning of the end of the string array 
    for(j = 0; string[i] != '<'; j++, i++) 
    { 
     s3[j] = string[i]; 
    } 

    // now insert one more null terminator for the final s array. 
    s3[j] = '\0'; 

    // now print out the newly fomrated strings. 
    printf("\nLoadAvg for 1min :: %s\n", s1); 
    printf("LoadAvg for 5min :: %s\n", s2); 
    printf("LoadAvg for 15min :: %s\n", s3); 

} 
+3

В 'receiveResponse', вы имели в виду' (* response) = malloc (sizeof (char)); 'Кажется, что это должно быть' (* response) = malloc (BUFFERSIZE); ' –

ответ

1
(*response) = malloc(sizeof(char)); 

// wait for a response from the server 
if(read(sock, (*response), BUFFERSIZE) < 0) 

Wh при размере BUFFERSIZE. Это больше, чем 1? Если это так, вы переполните свой буфер ...

+0

BUFFERSIZE = 256. Это максимально возможный размер отправляемого сообщения. – user1995624

+0

Да, поэтому ваш malloc, вероятно, будет '256', а не' 1' (sizeof (char) всегда '1'). Перезапись конца буферов, полученных из 'malloc', почти наверняка вызывает проблему в вашем коде. Я не видел никаких других вызовов 'malloc', но я, возможно, что-то пропустил. Проверьте все вызовы на 'malloc' и убедитесь, что размер достаточен для того, что вы придерживаетесь в буфере. –

+0

Благодарим вас за очевидную ошибку, которую я определенно должен был увидеть. Иногда это требует еще нескольких глаз. – user1995624

Смежные вопросы