From 2a0e64df46a811254e17abc4161914fc6217cd9c Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 21 Jan 2016 17:46:03 -0500 Subject: [PATCH] add run-time check to file_storage::map_block and improve its documentation --- include/libtorrent/file_storage.hpp | 9 ++++++++- src/file_storage.cpp | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/file_storage.hpp b/include/libtorrent/file_storage.hpp index 6a415d828..53dc61c6c 100644 --- a/include/libtorrent/file_storage.hpp +++ b/include/libtorrent/file_storage.hpp @@ -334,6 +334,13 @@ namespace libtorrent // returns a list of file_slice objects representing the portions of // files the specified piece index, byte offset and size range overlaps. // this is the inverse mapping of map_file(). + // + // Preconditions of this function is that the input range is within the + // torrents address space. ``piece`` may not be negative and + // + // ``piece`` * piece_size + ``offset`` + ``size`` + // + // may not exceed the total size of the torrent. std::vector map_block(int piece, boost::int64_t offset , int size) const; @@ -363,7 +370,7 @@ namespace libtorrent TORRENT_DEPRECATED reverse_iterator rend() const { return m_files.rend(); } TORRENT_DEPRECATED - internal_file_entry const& internal_at(int index) const + internal_file_entry const& internal_at(int index) const { TORRENT_ASSERT(index >= 0); TORRENT_ASSERT(index < int(m_files.size())); diff --git a/src/file_storage.cpp b/src/file_storage.cpp index 987b2bb61..db11be4f0 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -419,7 +419,8 @@ namespace libtorrent return m_files[index].name_len; } - std::vector file_storage::map_block(int piece, boost::int64_t offset + std::vector file_storage::map_block(int const piece + , boost::int64_t const offset , int size) const { TORRENT_ASSERT_PRECOND(num_files() > 0); @@ -433,6 +434,10 @@ namespace libtorrent TORRENT_ASSERT_PRECOND(boost::int64_t(target.offset + size) <= m_total_size); TORRENT_ASSERT(!compare_file_offset(target, m_files.front())); + // in case the size is past the end, fix it up + if (boost::int64_t(target.offset + size) > m_total_size) + size = m_total_size - target.offset; + std::vector::const_iterator file_iter = std::upper_bound( m_files.begin(), m_files.end(), target, compare_file_offset);