I have a "bus error" in the way I program. I am learning how to make a client /server with chained list. The error bus comes only when I return parse_message (...) = NULL
..
The listen_client function is in a thread, I do not know if it affects the error. If you want more details / other piece of code ask me.
Thank you and good evening.
static t_message *parse_message(char *buffer)
{
int id;
id = atoi(buffer);
printf("ID: %d\n", id);
return (NULL); // for sample
}
static char *read_message(char *data)
{
static char *buffer = NULL;
char *message = NULL;
char *ptr;
size_t size;
size = strlen(data);
if (buffer)
size += strlen(buffer);
if (!(buffer = realloc(buffer, size)))
return (NULL);
strcat(buffer, data);
if ((ptr = strstr(buffer, "\r\n")))
{
size = (ptr - buffer);
if (!(message = (char*)malloc(sizeof(char) * (size + 1))))
return (NULL);
strncpy(message, buffer, size);
ptr += 2;
if (!(*ptr))
buffer = NULL;
else if (!(buffer = strdup(ptr)))
return (NULL);
}
return (message);
}
void listen_client(t_args *args)
{
ssize_t count;
char buffer[BUFF_SIZE + 1] = { 0 };
char *msg_str;
t_message *msg;
while ((count = recv(args->client->s, buffer, BUFF_SIZE, 0)))
{
printf("===> %s\n", buffer);
if (!(msg_str = read_message(buffer)))
{
continue ;
}
if (!(msg = parse_message(msg_str)))
{
// bus error after continue ;
continue ;
}
handle_message();
}