From 674641accec771914f5c1af2774f58bf65a9b98e Mon Sep 17 00:00:00 2001 From: Andrei Kurushin Date: Thu, 12 May 2016 23:45:23 +0300 Subject: [PATCH] fix file_storage::m_paths memory reuse (#724) fix file_storage::m_paths memory reuse after rename_file with path delimiter --- src/file_storage.cpp | 8 ++++---- test/test_storage.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/file_storage.cpp b/src/file_storage.cpp index e46ee9cc6..6def9582e 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -168,6 +168,10 @@ namespace libtorrent // and the branch path branch_path = 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) { @@ -202,10 +206,6 @@ namespace libtorrent e.path_index = int(m_paths.size()); 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 m_paths.resize(m_paths.size() + 1); m_paths.back().assign(branch_path, branch_len); diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 2e271c626..c596433cc 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -1306,6 +1306,18 @@ TORRENT_TEST(move_storage_into_self) , 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) { std::string const save_path = combine_path(current_working_directory(), "save_path_1");