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

free():无效的下一个大小(快速):0x000000000060c0d0,c,C,我得到这个错误,我不知道为什么。有人能帮我吗? 这是我的密码: 私人信息.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); str

我得到这个错误,我不知道为什么。有人能帮我吗? 这是我的密码: 私人信息.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);
信息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
信息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.*/
#包括
#包括
#包括
#包括“message.h”
#包括“message private.h”
int message\u to\u缓冲区(结构message\u t*msg,char**msg\u buf){
如果(msg==NULL)
返回-1;
int buffSize=getSize(msg);
*msg_buf=(char*)malloc(buffSize);
如果(*msg_buf==NULL){
返回-1;
}
如果(消息->c_类型==CT_结果){
消息到缓冲区结果(消息,*msg\u buf);
}
否则如果(消息->c_类型==CT_值){
msg\u至缓冲区值(msg,*msg\u buf);
}
否则如果(消息->c_类型==CT_键){
消息到缓冲区键(消息,*msg\u buf);
}
否则如果(消息->c_类型==CT_键){
消息到缓冲区键(消息,*msg\u buf);
}
否则如果(消息->c_类型==CT_条目){
消息缓冲区条目(消息,*msg\u buf);
}
返回大小;
}
/*Transforma uma mensagem无数组数据字节、缓冲区、段落
*uma结构消息\u t*
*/
结构消息(char*msg\u buf,int msg\u size){
char*offset=msg_buf;
短op_码;
memcpy(操作码、偏移量、短尺寸);
op_代码=ntohs(op_代码);
//第二段缓冲区
如果(op_代码!=OC_大小和op_代码!=OC_删除和op_代码!=OC_更新和
操作码!=OC\u获取和操作码!=OC\u放置)
返回NULL;
偏移量+=\u短\u大小;
短c_型;
memcpy(&c_类型、偏移量、短尺寸);
c_类型=ntohs(c_类型);
//fazemos mais um teste para o c_型
如果(c_类型!=CT_结果和&c_类型!=CT_值和&c_类型!=CT_键和&
c_类型!=CT_键和&c_类型!=CT_项)
返回NULL;
如果(c_类型==CT_结果){
将缓冲区返回到消息结果(消息buf);
}
else if(c_类型==CT_值){
将缓冲区_返回到_msg_值(msg_buf);
}
else if(c_类型==CT_键){
将缓冲区返回到消息键(消息buf);
}
else if(c_类型==CT_键){
将缓冲区返回到消息键(消息buf);
}
else if(c_类型==CT_条目){
将缓冲区返回到消息项(消息buf);
}
}
/*自由阿罗卡达那方ção缓冲区信息
*/
无效自由消息(结构消息消息消息){
如果(msg!=NULL){
如果(消息->c_类型==CT_结果){
免费(味精);
}
否则如果(消息->c_类型==CT_值){
数据销毁(msg->content.data);
免费(味精);
}
否则如果(消息->c_类型==CT_键){
免费(味精);
}
否则如果(消息->c_类型==CT_键){
列出免费密钥(msg->content.keys);
免费(味精);
}
否则如果(消息->c_类型==CT_条目){
条目\销毁(msg->content.entry);
免费(味精);
}
}
}
int getSize(结构消息\u t*msg){
int size=4;
如果(消息->c_类型==CT_结果){
大小+=\u INT\u大小;
}否则如果(消息->c_类型==CT_值){
大小+=\u INT\u大小;
size+=msg->content.data->datasize;
}否则如果(消息->c_类型==CT_键){
大小+=\u短\u大小;
size+=strlen(msg->content.key);
}否则如果(消息->c_类型==CT_键){
大小+=\u INT\u大小;
int i;
对于(i=0;msg->content.keys[i]!=NULL;i++){
大小+=\u短\u大小;
size+=strlen(msg->content.keys[i]);
}
}否则如果(消息->c_类型==CT_条目){
大小+=\u短\u大小;
大小+=strlen(消息->内容.输入->键);
大小+=\u INT\u大小;
大小+=msg->content.entry->value->datasize;
}
返回大小;
}
/*Serializa uma estrutura消息com c_type=结果*/
void msg\u to\u buffer\u结果(结构消息\u t*msg,char*msg\u buf){
char*offset=msg_buf;
短操作码=HTON(消息->操作码);
memcpy(偏移量、操作码、短尺寸);
偏移量+=\u短\u大小;
短c_-type=htons(msg->c_-type);
memcpy(偏移量、c_类型、短尺寸);
偏移量+=\u短\u大小;
int result=htonl(msg->content.result);
memcpy(偏移量、结果、内部大小);
}
结构消息*缓冲区\到消息\结果(字符*消息\结果){
struct message_t*msg=(struct message_t*)malloc(sizeof(struct message_t));
char*offset=msg_buf;
短操作码=0;
memcpy(操作码、偏移量、短尺寸);
msg->opcode=ntohs(操作码);
偏移量+=\u短\u大小;
短c_类型=0;
memcpy(&c_类型、偏移量、短尺寸);
msg->c_type=ntohs(c_type);
偏移量+=\u短\u大小;
int结果=0;
memcpy(&结果、偏移量、整数大小);
msg->content.result=ntohl(结果);
返回味精;
}
/*Serializa uma estrutura消息com c_type=值*/
void msg_to_buffer_值(struct message_t*msg,char*msg_buf){
char*offset=msg_buf;
短操作码=HTON(消息->操作码);
memcpy(偏移量、操作码、短尺寸);
偏移量+=\u短\u大小;
短c_-type=htons(msg->c_-type);
memcpy(偏移量、c_类型、短尺寸);
偏移量+=\u短\u大小;
int data_size=htonl(msg->content.data->datasize);
memcpy(偏移量、数据大小和内部大小);
偏移量+=\u INT\u大小;
memcpy(offset,msg->content.data->data,msg->content.data->datasize);
}
结构消息缓冲区到消息值(char*msg\u buf){
struct message_t*msg=(struct message_t*)malloc(sizeof(struct message_t));
char*offset=msg_buf;
短操作码=0;
memcpy(操作码、偏移量、短尺寸);
msg->opcode=ntohs(操作码);
偏移量+=\u短\u大小;
短c_类型=0;
memcpy(&c_类型、偏移量、短尺寸);
msg->c_type=ntohs(c_type);
偏移量+=\u短\u大小;
int-datasize=0;
memcpy(数据大小、偏移量、整数大小);
msg->content.data->datasize=ntohl(datasize);
偏移量+=\u INT\u大小;
memcpy(msg->content.data->data,offset,msg->content.data->datasize);
返回味精;
}
/*Serializa uma estrutura消息com c_type=key*/
void msg\u to\u buffer\u键(结构消息\u t*msg,字符*msg\u buf){
char*offset=msg_buf;
短操作码=HTON(消息->操作码);
memcpy(偏移量、操作码、短尺寸);
偏移量+=\u短\u大小;
短c_-type=htons(msg->c_-type);
memcpy(偏移量、c_类型、短尺寸);
偏移量+=\u短\u大小;
short key_size=htons(strlen(msg->content.key));
memcpy(偏移量、键大小和短键大小);
偏移量+=\u短\u大小;
memcpy(offset,msg->content.key,key\u size);
}
结构消息\u t*缓冲区\u到\u消息\u键(char*msg\u buf){
struct message_t*msg=(struct message_t*)malloc(sizeof(struct message_t));
char*offset=msg_buf;
短操作码=0;
memcpy(操作码、偏移量、短尺寸);
msg->opcode=ntohs(操作码);
偏移量+=\u短\u大小;
短c_类型=0;
memcpy(&c_类型、偏移量、短尺寸);
msg->c_type=ntohs(c_type);
抵消+=
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;

}