Crash fix.

This commit is contained in:
Jan Vidar Krey 2009-06-30 22:15:08 +02:00
parent aa7be1dc4b
commit ee0ea5a427
1 changed files with 38 additions and 21 deletions

View File

@ -135,20 +135,18 @@ int handle_net_read(struct user* user)
{ {
static char buf[MAX_RECV_BUF]; static char buf[MAX_RECV_BUF];
struct hub_recvq* q = user->net.recv_queue; struct hub_recvq* q = user->net.recv_queue;
size_t buf_size; size_t buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF);
ssize_t size = net_user_recv(user, &buf[buf_size], MAX_RECV_BUF - buf_size);
buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF);
int size = net_user_recv(user, &buf[buf_size], MAX_RECV_BUF - buf_size);
if (size > 0) if (size > 0)
{
buf_size += size; buf_size += size;
}
if (size == -1) if (size == -1)
{ {
if (net_error() != EWOULDBLOCK || net_error() != EINTR) if (net_error() == EWOULDBLOCK || net_error() == EINTR)
return quit_socket_error; return 0;
return quit_socket_error;
} }
else if (size == 0) else if (size == 0)
{ {
@ -156,34 +154,53 @@ int handle_net_read(struct user* user)
} }
else else
{ {
size_t offset = 0; char* lastPos = 0;
size_t length;
char* start = buf; char* start = buf;
char* pos = 0; char* pos = 0;
while ((pos = memchr(start, '\n', (buf_size - offset)))) size_t remaining = buf_size;
while ((pos = memchr(start, '\n', remaining)))
{ {
char* line = start; lastPos = pos;
length = pos - start;
pos[0] = '\0'; pos[0] = '\0';
#ifdef DEBUG_SENDQ #ifdef DEBUG_SENDQ
printf("PROC: \"%s\" (%d)\n", line, (int) length); printf("PROC: \"%s\" (%d)\n", start, (int) (pos - start));
#endif #endif
if (hub_handle_message(g_hub, user, line, length) == -1) if (user_flag_get(user, flag_maxbuf))
{ {
return quit_protocol_error; user_flag_unset(user, flag_maxbuf);
break; }
else
{
if (g_hub->config->max_recv_buffer < (pos - start))
{
if (hub_handle_message(g_hub, user, start, (pos - start)) == -1)
{
return quit_protocol_error;
break;
}
}
} }
pos[0] = '\n'; /* FIXME: not needed */
pos ++;
remaining -= (pos - start);
start = pos; start = pos;
start++;
offset += length;
} }
if (start < buf + buf_size) if (lastPos)
{ {
hub_recvq_set(q, buf+offset, buf_size - offset); if (remaining < g_hub->config->max_recv_buffer)
{
hub_recvq_set(q, lastPos, remaining);
}
else
{
hub_recvq_set(q, 0, 0);
user_flag_set(user, flag_maxbuf);
}
} }
else else
{ {