free():无效的下一个大小(快):0x000000000060c0d0

时间:2015-10-31 19:27:58

标签: c

我收到此错误,我不知道为什么。有人能帮助我吗? 这是我的代码: 消息private.h:

#include "list.h"
#include "message.h"

#define _SHORT_SIZE 2
#define _INT_SIZE 4
#define OC_RT_ERROR 99

int getSize(struct message_t *msg);
void msg_to_buffer_result(struct message_t *msg, char *msg_buf);
struct message_t* buffer_to_msg_result(char *msg_buf);
void msg_to_buffer_value(struct message_t * msg, char *msg_buf);
struct message_t* buffer_to_msg_value(char *msg_buf);
void msg_to_buffer_key(struct message_t * msg, char *msg_buf);
struct message_t* buffer_to_msg_key(char *msg_buf);
void msg_to_buffer_keys(struct message_t * msg, char *msg_buf);
struct message_t* buffer_to_msg_keys(char *msg_buf);
void msg_to_buffer_entry(struct message_t * msg, char *msg_buf);
struct message_t* buffer_to_msg_entry(char *msg_buf);
int read_all(int connsockfd, char * str, int size);
int write_all(int sockfd, char * str, int size);

message.h:

#ifndef _MESSAGE_H
#define _MESSAGE_H

#include "data.h"
#include "entry.h"

/* Define os possíveis opcodes da mensagem */
#define OC_SIZE     10
#define OC_DEL      20
#define OC_UPDATE   30
#define OC_GET      40
#define OC_PUT      50

/* Define códigos para os possíveis conteúdos da mensagem */
#define CT_RESULT       10
#define CT_VALUE        20
#define CT_KEY          30
#define CT_KEYS         40
#define CT_ENTRY        50

/* Estrutura que representa uma mensagem genérica a ser transmitida.
 * Esta mensagem pode ter vários tipos de conteúdos.
 */
struct message_t {
    short opcode; /* código da operação na mensagem */
    short c_type; /* tipo do conteúdo da mensagem */
    union content_u {
       int result;
       struct data_t *data;
       char *key;
       char **keys;
       struct entry_t *entry;
} content;    /* conteúdo da mensagem */
};

/*
 * OPCODE       C_TYPE
 * [2 bytes]    [2 bytes]
 *  
 * a partir daí, o formato difere para cada tipo de conteúdo (c_type):
 * CT_ENTRY     KEYSIZE(KS)     KEY         DATASIZE(DS)    DATA
 *              [2 bytes]       [KS bytes]  [4 bytes]       [DS bytes]
 * CT_KEY       KEYSIZE(KS)     KEY         
 *              [2 bytes]       [KS bytes]
 * CT_KEYS      NKEYS           KEYSIZE(KS) KEY         ...
 *              [4 bytes]       [2 bytes]   [KS bytes]  ...
 * CT_VALUE     DATASIZE(DS)    DATA
 *              [4 bytes]       [DS bytes]
 * CT_RESULT    RESULT
 *              [4 bytes]
 *
 * Notar que o `\0´ no fim da string e o NULL no fim do array de
 * chaves não são enviados nas mensagens.
 */
int message_to_buffer(struct message_t *msg, char **msg_buf);

/* Transforma uma mensagem no array de bytes, buffer, para
 * uma struct message_t*
 */
 struct message_t *buffer_to_message(char *msg_buf, int msg_size);

 /* Liberta a memoria alocada na função buffer_to_message
  */
 void free_message(struct message_t *msg);

 #endif

message.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "message.h"
#include "message-private.h"

int message_to_buffer(struct message_t *msg, char **msg_buf){
if(msg == NULL)
    return -1;

int buffSize = getSize(msg);

*msg_buf = (char*)malloc(buffSize);
if(*msg_buf == NULL){
    return -1;
}

if(msg->c_type == CT_RESULT){

    msg_to_buffer_result(msg, *msg_buf);
}
else if(msg->c_type == CT_VALUE){
    msg_to_buffer_value(msg, *msg_buf);
}
else if(msg->c_type == CT_KEY){
    msg_to_buffer_key(msg, *msg_buf);
}
else if(msg->c_type == CT_KEYS){
    msg_to_buffer_keys(msg, *msg_buf);
}
else if(msg->c_type == CT_ENTRY){
    msg_to_buffer_entry(msg, *msg_buf);
}
return buffSize;
}

/* Transforma uma mensagem no array de bytes, buffer, para
 * uma struct message_t*
 */
struct message_t *buffer_to_message(char *msg_buf, int msg_size){
    char* offset = msg_buf; 
    short op_code;
    memcpy(&op_code, offset, _SHORT_SIZE);

op_code = ntohs(op_code);

//Defesa para um buffer bugado
if(op_code != OC_SIZE && op_code !=OC_DEL && op_code !=OC_UPDATE && 
    op_code !=OC_GET && op_code !=OC_PUT)
    return NULL;

offset += _SHORT_SIZE;

short c_type;
memcpy(&c_type,offset, _SHORT_SIZE);
c_type = ntohs(c_type);


//fazemos mais um teste para o c_type
if(c_type != CT_RESULT && c_type != CT_VALUE && c_type != CT_KEY && 
    c_type != CT_KEYS && c_type != CT_ENTRY )
    return NULL;

if(c_type == CT_RESULT){
    return buffer_to_msg_result(msg_buf);
}
else if(c_type == CT_VALUE){
    return buffer_to_msg_value(msg_buf);
}
else if(c_type == CT_KEY){
    return buffer_to_msg_key(msg_buf);
}
else if(c_type == CT_KEYS){
    return buffer_to_msg_keys(msg_buf);
}
else if(c_type == CT_ENTRY){
    return buffer_to_msg_entry(msg_buf);
}

}

/* Liberta a memoria alocada na função buffer_to_message
 */

void free_message(struct message_t *msg){
if(msg != NULL){
    if(msg->c_type == CT_RESULT){
        free(msg);
    }
    else if(msg->c_type == CT_VALUE){
        data_destroy(msg->content.data);
        free(msg);
    }
    else if(msg->c_type == CT_KEY){
        free(msg);
    }
    else if(msg->c_type == CT_KEYS){
        list_free_keys(msg->content.keys);
        free(msg);
    }
    else if(msg->c_type == CT_ENTRY){
        entry_destroy(msg->content.entry);
        free(msg);
     }
}
}






int getSize(struct message_t *msg){
int size = 4;

if(msg->c_type == CT_RESULT){
    size += _INT_SIZE;
}else if(msg->c_type == CT_VALUE){
    size += _INT_SIZE;
    size += msg->content.data->datasize;
}else if(msg->c_type == CT_KEY){
    size += _SHORT_SIZE;
    size += strlen(msg->content.key);
}else if(msg->c_type == CT_KEYS){
    size += _INT_SIZE;
    int i;
    for(i=0; msg->content.keys[i]!=NULL; i++){
        size+=_SHORT_SIZE;
        size+=strlen(msg->content.keys[i]);
    }
}else if(msg->c_type == CT_ENTRY){
    size += _SHORT_SIZE;
    size += strlen(msg->content.entry->key);
    size += _INT_SIZE;
    size += msg->content.entry->value->datasize;
}

return size;
}

/*Serializa uma estrutura message com c_type = result.*/
void msg_to_buffer_result(struct message_t *msg, char *msg_buf){
char* offset = msg_buf;

short opcode = htons(msg->opcode);
memcpy(offset, &opcode, _SHORT_SIZE);
offset+=_SHORT_SIZE;

short c_type = htons(msg->c_type);
memcpy(offset, &c_type, _SHORT_SIZE);
offset+=_SHORT_SIZE;

int result = htonl(msg->content.result);
memcpy(offset, &result, _INT_SIZE);
}

struct message_t* buffer_to_msg_result(char *msg_buf){
struct message_t * msg = (struct message_t*)malloc(sizeof(struct message_t));
char * offset = msg_buf;

short opcode = 0;
memcpy(&opcode, offset, _SHORT_SIZE);

msg->opcode = ntohs(opcode);
offset += _SHORT_SIZE;


short c_type = 0;
memcpy(&c_type, offset, _SHORT_SIZE);
msg->c_type = ntohs(c_type);
offset += _SHORT_SIZE;

int result = 0;
memcpy(&result, offset, _INT_SIZE);
msg->content.result = ntohl(result);

return msg;

}

 /*Serializa uma estrutura message com c_type = value.*/
void msg_to_buffer_value(struct message_t * msg, char *msg_buf){
char* offset= msg_buf;

short opcode = htons(msg->opcode);
memcpy(offset, &opcode , _SHORT_SIZE);
offset+=_SHORT_SIZE;

short c_type = htons(msg->c_type);
memcpy(offset, &c_type, _SHORT_SIZE);
offset+=_SHORT_SIZE;

int data_size = htonl(msg->content.data->datasize);
memcpy(offset, &data_size, _INT_SIZE);
offset+=_INT_SIZE;

memcpy(offset,msg->content.data->data, msg->content.data->datasize);
}

struct message_t* buffer_to_msg_value(char *msg_buf){
struct message_t * msg = (struct message_t*)malloc(sizeof(struct message_t));
char * offset = msg_buf;

short opcode = 0;
memcpy(&opcode, offset, _SHORT_SIZE);
msg->opcode = ntohs(opcode);
offset += _SHORT_SIZE;

short c_type = 0;
memcpy(&c_type, offset, _SHORT_SIZE);
msg->c_type = ntohs(c_type);
offset += _SHORT_SIZE;

int datasize = 0;
memcpy(&datasize, offset, _INT_SIZE);
msg->content.data->datasize = ntohl(datasize);
offset += _INT_SIZE;

memcpy(msg->content.data->data, offset, msg->content.data->datasize);
return msg;

}

/*Serializa uma estrutura message com c_type = key.*/
void msg_to_buffer_key(struct message_t * msg, char *msg_buf){
char* offset= msg_buf;

short opcode = htons(msg->opcode);
memcpy(offset, &opcode , _SHORT_SIZE);
offset+=_SHORT_SIZE;

short c_type = htons(msg->c_type);
memcpy(offset, &c_type, _SHORT_SIZE);
offset+=_SHORT_SIZE;


short key_size= htons(strlen(msg->content.key));
memcpy(offset, &key_size, _SHORT_SIZE);
offset+=_SHORT_SIZE;

memcpy(offset, msg->content.key, key_size);
}

struct message_t* buffer_to_msg_key(char *msg_buf){
struct message_t * msg = (struct message_t*)malloc(sizeof(struct message_t));
char * offset = msg_buf;

short opcode = 0;
memcpy(&opcode, offset, _SHORT_SIZE);
msg->opcode = ntohs(opcode);
offset += _SHORT_SIZE;

short c_type = 0;
memcpy(&c_type, offset, _SHORT_SIZE);
msg->c_type = ntohs(c_type);
offset += _SHORT_SIZE;
short key_size = 0;
memcpy(&key_size, offset, _SHORT_SIZE);
key_size = ntohs(key_size);
offset += _SHORT_SIZE;

msg->content.key = (char*)malloc(key_size +1);

memcpy(msg->content.key, offset, key_size);
strcat(msg->content.key, "\0");
return msg;

}

/*Serializa uma estrutura message com c_type = keys.*/

void msg_to_buffer_keys(struct message_t * msg,char * msg_buf){     char * offset = msg_buf;

short opcode = htons(msg->opcode);
memcpy(offset, &opcode , _SHORT_SIZE);
offset+=_SHORT_SIZE;

short c_type = htons(msg->c_type);
memcpy(offset, &c_type, _SHORT_SIZE);
offset+=_SHORT_SIZE;

int i;
int n_keys = 0;
for(i=0; msg->content.keys[i]!=NULL; i++){}
n_keys = i-1;
n_keys = htonl(n_keys);
memcpy(offset, &n_keys, _INT_SIZE);
offset+=_INT_SIZE;

int length = 0;
int encryptL = 0;
for(i=0; msg->content.keys[i]!=NULL; i++){
    length=strlen(msg->content.keys[i]);
    encryptL = htons(length);
    memcpy(offset, &encryptL, _SHORT_SIZE);
    offset+=_SHORT_SIZE;

    memcpy(offset, msg->content.keys[i], length);
    offset+=length;
    printf("\n %s", msg->content.keys[i]);

}

}

struct message_t* buffer_to_msg_keys(char *msg_buf){
struct message_t * msg = (struct message_t*)malloc(sizeof(struct   message_t));
char * offset = msg_buf;

memcpy(&msg->opcode, offset, _SHORT_SIZE);
msg->opcode = ntohs(msg->opcode);
offset += _SHORT_SIZE;

memcpy(&msg->c_type, offset, _SHORT_SIZE);
msg->c_type = ntohs(msg->c_type);
offset += _SHORT_SIZE;

int n_keys = 0;
memcpy(&n_keys, offset, _INT_SIZE);
n_keys = ntohl(n_keys);
n_keys++;
offset += _INT_SIZE;

printf("\n testeD");
msg->content.keys = (char**)malloc(n_keys*sizeof(char*));
if(msg->content.keys == NULL){
    printf("Erro a alocar memoria para as chaves");
    return NULL;
}

printf("\n %d", n_keys);
int i;
int length = 0;
for(i=0; i < n_keys; i++){
    memcpy(&length, offset, _SHORT_SIZE);
    length = ntohs(length);
    offset+=_SHORT_SIZE;
    msg->content.keys[i] = (char*)malloc(length + 1);

    memcpy(msg->content.keys[i], offset, length);
    msg->content.keys[i][length+1] = '\0';
    offset+=length;
    printf("\n %s", msg->content.keys[i]);

}
printf("\n testeF");
msg->content.keys[n_keys] = NULL;
printf("\n testeG");
return msg;

}



/*Serializa uma estrutura message com c_type = entry.*/
void msg_to_buffer_entry(struct message_t * msg, char *msg_buf){
char* offset= msg_buf;

short opcode = htons(msg->opcode);
memcpy(offset, &opcode , _SHORT_SIZE);
offset+=_SHORT_SIZE;

short c_type = htons(msg->c_type);
memcpy(offset, &c_type, _SHORT_SIZE);
offset+=_SHORT_SIZE;

short key_size=htons(strlen(msg->content.entry->key));
memcpy(offset, &key_size, _SHORT_SIZE);
offset+=_SHORT_SIZE;

memcpy(offset, msg->content.entry->key,strlen(msg->content.entry->key));
offset+=(strlen(msg->content.entry->key));

int data_size=htonl(msg->content.entry->value->datasize);
memcpy(offset, &data_size, _INT_SIZE);
offset+=_INT_SIZE;

memcpy(offset, msg->content.entry->value->data, msg->content.entry->value->datasize);

}

struct message_t* buffer_to_msg_entry(char *msg_buf){
struct message_t * msg = (struct message_t*)malloc(sizeof(struct message_t));
if(msg == NULL) return NULL;
char * offset = msg_buf;

short opcode = 0;
memcpy(&opcode, offset, _SHORT_SIZE);
msg->opcode = ntohs(opcode);
offset += _SHORT_SIZE;

short c_type = 0;
memcpy(&c_type, offset, _SHORT_SIZE);
msg->c_type = ntohs(c_type);
offset += _SHORT_SIZE;

short key_size = 0;
memcpy(&key_size, offset, _SHORT_SIZE);
key_size = ntohs(key_size);
offset += _SHORT_SIZE;

char *key = (char*)malloc((key_size +1));

if(msg->content.entry->key == NULL){
    printf("\nErro a alocar mem para a chave");
    return NULL;
}


memcpy(key, offset, key_size); 
key[key_size] = '\0';
offset += key_size;

int datasize = 0;
memcpy(&datasize, offset, _INT_SIZE);
struct data_t *data = data_create(ntohl(datasize));
offset += _INT_SIZE;

memcpy(data->data, offset, data->datasize);
msg->content.entry = entry_create(key, data);

return msg;

}

0 个答案:

没有答案