From 205d70da3c9f588b67428463a1a6e1ea16f14fb3 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 25 Nov 2012 07:03:37 +0000 Subject: [PATCH] merged uTP fix from RC_0_16 --- include/libtorrent/packet_buffer.hpp | 2 +- src/packet_buffer.cpp | 2 +- test/test_primitives.cpp | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/packet_buffer.hpp b/include/libtorrent/packet_buffer.hpp index 0e46fe3cf..759ee27d9 100644 --- a/include/libtorrent/packet_buffer.hpp +++ b/include/libtorrent/packet_buffer.hpp @@ -107,7 +107,7 @@ namespace libtorrent std::size_t m_size; // This defines the first index that is part of the m_storage. - // The last index is (m_first + (m_capacity - 1)) & 0xffff. + // last is one passed the last used slot index_type m_first; index_type m_last; }; diff --git a/src/packet_buffer.cpp b/src/packet_buffer.cpp index 68e2d2d8d..da8eea280 100644 --- a/src/packet_buffer.cpp +++ b/src/packet_buffer.cpp @@ -106,7 +106,7 @@ namespace libtorrent { // We have wrapped. if (idx >= ((m_first + m_capacity) & 0xffff) && m_capacity < 0xffff) { - reserve(m_capacity + (idx - ((m_first + m_capacity) & 0xffff))); + reserve(m_capacity + (idx + 1 - ((m_first + m_capacity) & 0xffff))); } } if (compare_less_wrap(m_last, (idx + 1) & 0xffff, 0xffff)) diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 407cf0ce6..52a3bccef 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -705,6 +705,25 @@ int test_main() TEST_CHECK(pb.at(2) == (void*)2); } + { + // test wrapping the indices + packet_buffer pb; + + TEST_EQUAL(pb.size(), 0); + + pb.insert(0xfff3, (void*)1); + TEST_CHECK(pb.at(0xfff3) == (void*)1); + + int new_index = (0xfff3 + pb.capacity()) & 0xffff; + pb.insert(new_index, (void*)2); + TEST_CHECK(pb.at(new_index) == (void*)2); + + void* old = pb.remove(0xfff3); + TEST_CHECK(old == (void*)1); + TEST_CHECK(pb.at(0xfff3) == (void*)0); + TEST_CHECK(pb.at(new_index) == (void*)2); + } + TEST_CHECK(error_code(errors::http_error).message() == "HTTP error"); TEST_CHECK(error_code(errors::missing_file_sizes).message() == "missing or invalid 'file sizes' entry"); TEST_CHECK(error_code(errors::unsupported_protocol_version).message() == "unsupported protocol version");