From: Nick Mathewson Date: Fri, 26 Mar 2010 18:51:39 +0000 (-0400) Subject: Make evbuffer_prepend handle empty buffers better X-Git-Tag: release-2.0.5-beta~70 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c87272b7b91b4f443092eb2a81f03c83457ab03a;p=libevent Make evbuffer_prepend handle empty buffers better 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. --- diff --git a/buffer.c b/buffer.c index 06974bad..5cbd10b1 100644 --- 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);