write_queue: Only pop the queue if it is not empty
authorHolger Hans Peter Freyther <zecke@selfish.org>
Mon, 14 Feb 2011 23:42:19 +0000 (00:42 +0100)
committerHolger Hans Peter Freyther <zecke@selfish.org>
Tue, 15 Feb 2011 00:01:44 +0000 (01:01 +0100)
It is possible that the queue is cleared after the select
and before the callback for writable is called. Check if
the list is not empty brefore taking an item out of it.

src/write_queue.c

index 618a8c0..7295569 100644 (file)
@@ -39,16 +39,18 @@ int write_queue_bfd_cb(struct bsc_fd *fd, unsigned int what)
                struct msgb *msg;
 
                fd->when &= ~BSC_FD_WRITE;
-               msg = msgb_dequeue(&queue->msg_queue);
-               if (!msg)
-                       return -1;
 
-               --queue->current_length;
-               queue->write_cb(fd, msg);
-               msgb_free(msg);
+               /* the queue might have been emptied */
+               if (!llist_empty(&queue->msg_queue)) {
+                       --queue->current_length;
+
+                       msg = msgb_dequeue(&queue->msg_queue);
+                       queue->write_cb(fd, msg);
+                       msgb_free(msg);
 
-               if (!llist_empty(&queue->msg_queue))
-                       fd->when |= BSC_FD_WRITE;
+                       if (!llist_empty(&queue->msg_queue))
+                               fd->when |= BSC_FD_WRITE;
+               }
        }
 
        return 0;