fix file_storage::m_paths memory reuse (#724)

fix file_storage::m_paths memory reuse after rename_file with path delimiter
This commit is contained in:
Andrei Kurushin 2016-05-12 23:45:23 +03:00 committed by Arvid Norberg
parent 7c108e6c19
commit 674641acce
2 changed files with 16 additions and 4 deletions

View File

@ -168,6 +168,10 @@ namespace libtorrent
// and the branch path // and the branch path
branch_path = path.c_str(); branch_path = path.c_str();
branch_len = leaf - path.c_str(); branch_len = leaf - path.c_str();
// trim trailing slashes
if (branch_len > 0 && branch_path[branch_len-1] == TORRENT_SEPARATOR)
--branch_len;
} }
if (branch_len <= 0) if (branch_len <= 0)
{ {
@ -202,10 +206,6 @@ namespace libtorrent
e.path_index = int(m_paths.size()); e.path_index = int(m_paths.size());
TORRENT_ASSERT(branch_path[0] != '/'); TORRENT_ASSERT(branch_path[0] != '/');
// trim trailing slashes
if (branch_len > 0 && branch_path[branch_len-1] == TORRENT_SEPARATOR)
--branch_len;
// poor man's emplace back // poor man's emplace back
m_paths.resize(m_paths.size() + 1); m_paths.resize(m_paths.size() + 1);
m_paths.back().assign(branch_path, branch_len); m_paths.back().assign(branch_path, branch_len);

View File

@ -1306,6 +1306,18 @@ TORRENT_TEST(move_storage_into_self)
, combine_path("_folder3", "test4.tmp"))))); , combine_path("_folder3", "test4.tmp")))));
} }
TORRENT_TEST(storage_paths_string_pooling)
{
file_storage file_storage;
file_storage.add_file(combine_path("test_storage", "root.txt"), 0x4000);
file_storage.add_file(combine_path("test_storage", combine_path("sub", "test1.txt")), 0x4000);
file_storage.add_file(combine_path("test_storage", combine_path("sub", "test2.txt")), 0x4000);
file_storage.add_file(combine_path("test_storage", combine_path("sub", "test3.txt")), 0x4000);
// "sub" paths should point to same string item, so paths.size() must not grow
TEST_CHECK(file_storage.paths().size() <= 2);
}
TORRENT_TEST(dont_move_intermingled_files) TORRENT_TEST(dont_move_intermingled_files)
{ {
std::string const save_path = combine_path(current_working_directory(), "save_path_1"); std::string const save_path = combine_path(current_working_directory(), "save_path_1");