add run-time check to file_storage::map_block and improve its documentation

This commit is contained in:
arvidn 2016-01-21 17:46:03 -05:00
parent f662b633c3
commit 2a0e64df46
2 changed files with 14 additions and 2 deletions

View File

@ -334,6 +334,13 @@ namespace libtorrent
// returns a list of file_slice objects representing the portions of // returns a list of file_slice objects representing the portions of
// files the specified piece index, byte offset and size range overlaps. // files the specified piece index, byte offset and size range overlaps.
// this is the inverse mapping of map_file(). // 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<file_slice> map_block(int piece, boost::int64_t offset std::vector<file_slice> map_block(int piece, boost::int64_t offset
, int size) const; , int size) const;
@ -363,7 +370,7 @@ namespace libtorrent
TORRENT_DEPRECATED TORRENT_DEPRECATED
reverse_iterator rend() const { return m_files.rend(); } reverse_iterator rend() const { return m_files.rend(); }
TORRENT_DEPRECATED 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 >= 0);
TORRENT_ASSERT(index < int(m_files.size())); TORRENT_ASSERT(index < int(m_files.size()));

View File

@ -419,7 +419,8 @@ namespace libtorrent
return m_files[index].name_len; return m_files[index].name_len;
} }
std::vector<file_slice> file_storage::map_block(int piece, boost::int64_t offset std::vector<file_slice> file_storage::map_block(int const piece
, boost::int64_t const offset
, int size) const , int size) const
{ {
TORRENT_ASSERT_PRECOND(num_files() > 0); 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_PRECOND(boost::int64_t(target.offset + size) <= m_total_size);
TORRENT_ASSERT(!compare_file_offset(target, m_files.front())); 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<internal_file_entry>::const_iterator file_iter = std::upper_bound( std::vector<internal_file_entry>::const_iterator file_iter = std::upper_bound(
m_files.begin(), m_files.end(), target, compare_file_offset); m_files.begin(), m_files.end(), target, compare_file_offset);