]> granicus.if.org Git - libevent/commitdiff
buffer: evbuffer_add_buffer(): clean empty chains from destination buffer
authorAzat Khuzhin <a3at.mail@gmail.com>
Tue, 7 Jun 2016 15:31:48 +0000 (18:31 +0300)
committerAzat Khuzhin <a3at.mail@gmail.com>
Fri, 17 Jun 2016 14:41:03 +0000 (17:41 +0300)
@EMPanisset reported a problem (#358) with evbuffer_remove_buffer(), but
actually I think that the problem is in evbuffer_add_buffer() which introduces
this empty chain, all other callers (except evbuffer_prepend_buffer(), but it
doesn't have this problem though) should be safe.

And FWIW the only API that allows empty chains is evbuffer_add_reference(), and
we can add check there to avoid such issues, but for now I leaved this without
fixing, since I think that evbuffer_add_reference() with empty chains can be
used as a barrier (but this can be tricky).

Fixes: regress evbuffer/remove_buffer_with_empty2
v2: introduce/fixes evbuffer/add_buffer_with_empty

buffer.c

index c5f1b8a5eb9d1572a29c9136a4e5fd184d46ddd8..6786f9024c7724f7dffa18eaad960fb539c427df 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -885,9 +885,13 @@ APPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
 {
        ASSERT_EVBUFFER_LOCKED(dst);
        ASSERT_EVBUFFER_LOCKED(src);
-       dst->last->next = src->first;
+
+       struct evbuffer_chain **chp;
+       chp = evbuffer_free_trailing_empty_chains(dst);
+       *chp = src->first;
+
        if (src->last_with_datap == &src->first)
-               dst->last_with_datap = &dst->last->next;
+               dst->last_with_datap = chp;
        else
                dst->last_with_datap = src->last_with_datap;
        dst->last = src->last;