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.
This commit is contained in:
Nick Mathewson 2010-03-26 14:51:39 -04:00
parent 5c0ebb33f4
commit c87272b7b9

View File

@ -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);