From c986f2321dc49c927435fadbc404eb9ffd1b1bab Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Thu, 8 Dec 2011 14:30:20 -0500 Subject: [PATCH 1/2] Fix behavior of evbuffer_peek(buf,-1,NULL,NULL,0) (Patch altered by nickm to not affect the behavior of evbuffer_peek(buf,-1,NULL,vec,n_vec).) --- buffer.c | 9 ++++++++- test/regress_buffer.c | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/buffer.c b/buffer.c index a042a1ed..d048a622 100644 --- a/buffer.c +++ b/buffer.c @@ -2582,14 +2582,21 @@ evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len, chain = buffer->first; } + if (n_vec == 0 && len < 0) { + /* If no vectors are provided and they asked for "everything", + * pretend they asked for the actual available amount. */ + len = buffer->total_len - len_so_far; + } + while (chain) { if (len >= 0 && len_so_far >= len) break; if (idxbuffer + chain->misalign; vec[idx].iov_len = chain->off; - } else if (len<0) + } else if (len<0) { break; + } ++idx; len_so_far += chain->off; chain = chain->next; diff --git a/test/regress_buffer.c b/test/regress_buffer.c index e51770df..addd5f9a 100644 --- a/test/regress_buffer.c +++ b/test/regress_buffer.c @@ -1441,6 +1441,10 @@ test_evbuffer_peek(void *info) evbuffer_add_buffer(buf, tmp_buf); } + /* How many chunks do we need for everything? */ + i = evbuffer_peek(buf, -1, NULL, NULL, 0); + tt_int_op(i, ==, 16); + /* Simple peek: get everything. */ i = evbuffer_peek(buf, -1, NULL, v, 20); tt_int_op(i, ==, 16); /* we used only 16 chunks. */ From 7bbf6ca7712bac9081d955c0f673691122ccbf5c Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 8 Dec 2011 14:36:32 -0500 Subject: [PATCH 2/2] Slightly clarify evbuffer_peek documentation --- include/event2/buffer.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/event2/buffer.h b/include/event2/buffer.h index d28cd2dc..32963568 100644 --- a/include/event2/buffer.h +++ b/include/event2/buffer.h @@ -626,8 +626,10 @@ struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer, the buffer does not have as much data as you asked to see). @param buffer the evbuffer to peek into, - @param len the number of bytes to try to peek. If negative, we - will try to fill as much of vec_out as we can. + @param len the number of bytes to try to peek. If len is negative, we + will try to fill as much of vec_out as we can. If len is negative + and vec_out is not provided, we return the number of evbuffer_iovecs + that would be needed to get all the data in the buffer. @param start_at an evbuffer_ptr indicating the point at which we should start looking for data. NULL means, "At the start of the buffer."