]> granicus.if.org Git - libevent/commitdiff
Make evbuffer_prepend handle empty buffers better
authorNick Mathewson <nickm@torproject.org>
Fri, 26 Mar 2010 18:51:39 +0000 (14:51 -0400)
committerNick Mathewson <nickm@torproject.org>
Fri, 26 Mar 2010 18:51:39 +0000 (14:51 -0400)
If the first chunk of a buffer is empty, and we're told to prepend to
the buffer, we should be willing to use the entire first chunk.
Instead, we were dependent on the value of chunk->misalign.

buffer.c

index 06974bad05a57531348bd962b19e83b6d571c904..5cbd10b19f6d5ee2dd40bb02dffdaada3ee90c7f 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -1398,8 +1398,13 @@ evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen)
 
        /* we cannot touch immutable buffers */
        if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) {
+               /* If this chain is empty, we can treat it as
+                * 'empty at the beginning' rather than 'empty at the end' */
+               if (chain->off == 0)
+                       chain->misalign = chain->buffer_len;
+
                if ((size_t)chain->misalign >= datlen) {
-                       /* we have enough space */
+                       /* we have enough space to fit everything */
                        memcpy(chain->buffer + chain->misalign - datlen,
                            data, datlen);
                        chain->off += datlen;
@@ -1408,6 +1413,7 @@ evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen)
                        buf->n_add_for_cb += datlen;
                        goto out;
                } else if (chain->misalign) {
+                       /* we can only fit some of the data. */
                        memcpy(chain->buffer,
                            (char*)data + datlen - chain->misalign,
                            chain->misalign);