fix crash when creating torrents and optimizing file order with pad files

This commit is contained in:
Arvid Norberg 2012-08-12 19:16:20 +00:00
parent a84c5e0e85
commit 1f32bdab5a
2 changed files with 21 additions and 1 deletions

View File

@ -1,6 +1,7 @@
* fix uTP edge case where udp socket buffer fills up * fix uTP edge case where udp socket buffer fills up
* fix nagle implementation in uTP * fix nagle implementation in uTP
* fix crash when creating torrents and optimizing file order with pad files
* disable support for large MTUs in uTP until it is more reliable * disable support for large MTUs in uTP until it is more reliable
* expose post_torrent_updates and state_update_alert to python bindings * expose post_torrent_updates and state_update_alert to python bindings
* fix incorrect SSL error messages * fix incorrect SSL error messages

View File

@ -490,31 +490,40 @@ namespace libtorrent
void file_storage::reorder_file(int index, int dst) void file_storage::reorder_file(int index, int dst)
{ {
TORRENT_ASSERT(index < int(m_files.size()));
TORRENT_ASSERT(dst < int(m_files.size()));
TORRENT_ASSERT(dst < index);
internal_file_entry e = m_files[index]; internal_file_entry e = m_files[index];
m_files.erase(m_files.begin() + index); m_files.erase(m_files.begin() + index);
m_files.insert(m_files.begin() + dst, e); m_files.insert(m_files.begin() + dst, e);
if (!m_mtime.empty()) if (!m_mtime.empty())
{ {
TORRENT_ASSERT(m_mtime.size() == m_files.size());
time_t mtime = 0; time_t mtime = 0;
if (int(m_mtime.size()) > index) if (int(m_mtime.size()) > index)
{ {
mtime = m_mtime[index]; mtime = m_mtime[index];
m_mtime.erase(m_mtime.begin() + index); m_mtime.erase(m_mtime.begin() + index);
} }
if (dst > int(m_mtime.size())) m_mtime.resize(dst, 0);
m_mtime.insert(m_mtime.begin() + dst, mtime); m_mtime.insert(m_mtime.begin() + dst, mtime);
} }
if (!m_file_hashes.empty()) if (!m_file_hashes.empty())
{ {
TORRENT_ASSERT(m_file_hashes.size() == m_files.size());
char const* fh = 0; char const* fh = 0;
if (int(m_file_hashes.size()) > index) if (int(m_file_hashes.size()) > index)
{ {
fh = m_file_hashes[index]; fh = m_file_hashes[index];
m_file_hashes.erase(m_file_hashes.begin() + index); m_file_hashes.erase(m_file_hashes.begin() + index);
} }
if (int(m_file_hashes.size()) < dst) m_file_hashes.resize(dst, NULL);
m_file_hashes.insert(m_file_hashes.begin() + dst, fh); m_file_hashes.insert(m_file_hashes.begin() + dst, fh);
} }
if (!m_file_base.empty()) if (!m_file_base.empty())
{ {
TORRENT_ASSERT(m_file_base.size() == m_files.size());
size_type base = 0; size_type base = 0;
if (int(m_file_base.size()) > index) if (int(m_file_base.size()) > index)
{ {
@ -589,7 +598,6 @@ namespace libtorrent
int cur_index = file_index(*i); int cur_index = file_index(*i);
reorder_file(index, cur_index); reorder_file(index, cur_index);
i = m_files.begin() + cur_index; i = m_files.begin() + cur_index;
i->offset = off; i->offset = off;
off += i->size; off += i->size;
continue; continue;
@ -601,6 +609,7 @@ namespace libtorrent
// new pad file. Once we're done adding it, we need // new pad file. Once we're done adding it, we need
// to increment i to point to the current file again // to increment i to point to the current file again
internal_file_entry e; internal_file_entry e;
int cur_index = file_index(*i);
i = m_files.insert(i, e); i = m_files.insert(i, e);
i->size = pad_size; i->size = pad_size;
i->offset = off; i->offset = off;
@ -611,6 +620,16 @@ namespace libtorrent
i->pad_file = true; i->pad_file = true;
off += pad_size; off += pad_size;
++padding_file; ++padding_file;
if (int(m_mtime.size()) > cur_index)
m_mtime.insert(m_mtime.begin() + cur_index, 0);
if (int(m_file_hashes.size()) > cur_index)
m_file_hashes.insert(m_file_hashes.begin() + cur_index, NULL);
if (int(m_file_base.size()) > cur_index)
m_file_base.insert(m_file_base.begin() + cur_index, 0);
// skip the pad file we just added and point // skip the pad file we just added and point
// at the current file again // at the current file again
++i; ++i;