Merge branch 'evbufer_peek_fix_v3'

This commit is contained in:
Nick Mathewson 2014-11-30 11:09:50 -05:00
commit a5d43cf4c6
2 changed files with 41 additions and 1 deletions

View File

@ -2742,7 +2742,10 @@ evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
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;
len = buffer->total_len;
if (start_at) {
len -= start_at->pos;
}
}
while (chain) {

View File

@ -1843,6 +1843,42 @@ end:
}
static void
test_evbuffer_peek_first_gt(void *info)
{
struct evbuffer *buf = NULL, *tmp_buf = NULL;
struct evbuffer_ptr ptr;
struct evbuffer_iovec v[2];
buf = evbuffer_new();
tmp_buf = evbuffer_new();
evbuffer_add_printf(tmp_buf, "Contents of chunk 100\n");
evbuffer_add_buffer(buf, tmp_buf);
evbuffer_add_printf(tmp_buf, "Contents of chunk 1\n");
evbuffer_add_buffer(buf, tmp_buf);
evbuffer_ptr_set(buf, &ptr, 0, EVBUFFER_PTR_SET);
/** The only case that matters*/
tt_int_op(evbuffer_peek(buf, -1, &ptr, NULL, 0), ==, 2);
/** Just in case */
tt_int_op(evbuffer_peek(buf, -1, &ptr, v, 2), ==, 2);
evbuffer_ptr_set(buf, &ptr, 20, EVBUFFER_PTR_ADD);
tt_int_op(evbuffer_peek(buf, -1, &ptr, NULL, 0), ==, 2);
tt_int_op(evbuffer_peek(buf, -1, &ptr, v, 2), ==, 2);
tt_int_op(evbuffer_peek(buf, 2, &ptr, NULL, 0), ==, 1);
tt_int_op(evbuffer_peek(buf, 2, &ptr, v, 2), ==, 1);
tt_int_op(evbuffer_peek(buf, 3, &ptr, NULL, 0), ==, 2);
tt_int_op(evbuffer_peek(buf, 3, &ptr, v, 2), ==, 2);
end:
if (buf)
evbuffer_free(buf);
if (tmp_buf)
evbuffer_free(tmp_buf);
}
static void
test_evbuffer_peek(void *info)
{
@ -2210,6 +2246,7 @@ struct testcase_t evbuffer_testcases[] = {
{ "multicast_drain", test_evbuffer_multicast_drain, 0, NULL, NULL },
{ "prepend", test_evbuffer_prepend, TT_FORK, NULL, NULL },
{ "peek", test_evbuffer_peek, 0, NULL, NULL },
{ "peek_first_gt", test_evbuffer_peek_first_gt, 0, NULL, NULL },
{ "freeze_start", test_evbuffer_freeze, 0, &nil_setup, (void*)"start" },
{ "freeze_end", test_evbuffer_freeze, 0, &nil_setup, (void*)"end" },
{ "add_iovec", test_evbuffer_add_iovec, 0, NULL, NULL},