Skip to content

Commit 5026659

Browse files
committed
clean up and fix edge cases in update_path_index
1 parent 17d78d3 commit 5026659

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

include/libtorrent/file_storage.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ namespace libtorrent {
509509
// low-level function. returns a pointer to the internal storage for
510510
// the filename. This string may not be 0-terminated!
511511
// the ``file_name_len()`` function returns the length of the filename.
512+
// prefer to use ``file_name()`` instead, which returns a ``string_view``.
512513
char const* file_name_ptr(file_index_t index) const;
513514
int file_name_len(file_index_t index) const;
514515

src/file_storage.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -131,42 +131,45 @@ namespace {
131131
// sorry about this messy string handling, but I did
132132
// profile it, and it was expensive
133133
char const* leaf = filename_cstr(path.c_str());
134-
char const* branch_path = "";
135-
int branch_len = 0;
134+
string_view branch_path;
136135
if (leaf > path.c_str())
137136
{
138137
// split the string into the leaf filename
139138
// and the branch path
140-
branch_path = path.c_str();
141-
branch_len = int(leaf - path.c_str());
139+
branch_path = path;
140+
branch_path = branch_path.substr(0
141+
, static_cast<std::size_t>(leaf - path.c_str()));
142142

143143
// trim trailing slashes
144-
if (branch_len > 0 && branch_path[branch_len - 1] == TORRENT_SEPARATOR)
145-
--branch_len;
144+
while (!branch_path.empty() && branch_path.back() == TORRENT_SEPARATOR)
145+
{
146+
branch_path.remove_suffix(1);
147+
}
146148
}
147-
if (branch_len <= 0)
149+
if (branch_path.empty())
148150
{
149151
if (set_name) e.set_name(leaf);
150152
e.path_index = -1;
151153
return;
152154
}
153155

154-
if (branch_len >= int(m_name.size())
155-
&& std::memcmp(branch_path, m_name.c_str(), m_name.size()) == 0
156+
if (branch_path.size() >= m_name.size()
157+
&& branch_path.substr(0, m_name.size()) == m_name
156158
&& branch_path[m_name.size()] == TORRENT_SEPARATOR)
157159
{
158-
int const offset = int(m_name.size())
159-
+ (int(m_name.size()) == branch_len ? 0 : 1);
160-
branch_path += offset;
161-
branch_len -= offset;
160+
branch_path.remove_prefix(m_name.size());
161+
while (!branch_path.empty() && branch_path.front() == TORRENT_SEPARATOR)
162+
{
163+
branch_path.remove_prefix(1);
164+
}
162165
e.no_root_dir = false;
163166
}
164167
else
165168
{
166169
e.no_root_dir = true;
167170
}
168171

169-
e.path_index = get_or_add_path({branch_path, aux::numeric_cast<std::size_t>(branch_len)});
172+
e.path_index = get_or_add_path(branch_path);
170173
if (set_name) e.set_name(leaf);
171174
}
172175

test/test_file_storage.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ TORRENT_TEST(pointer_offset)
183183
// test filename_ptr and filename_len
184184
TEST_EQUAL(st.file_name_ptr(file_index_t{0}), filename);
185185
TEST_EQUAL(st.file_name_len(file_index_t{0}), 5);
186+
TEST_EQUAL(st.file_name(file_index_t{0}), string_view(filename, 5));
186187

187188
TEST_EQUAL(st.file_path(file_index_t{0}, ""), combine_path("test-torrent-1", "test1"));
188189
TEST_EQUAL(st.file_path(file_index_t{0}, "tmp"), combine_path("tmp"
@@ -202,6 +203,32 @@ TORRENT_TEST(pointer_offset)
202203
TEST_EQUAL(st.file_name_len(file_index_t{0}), 5);
203204
}
204205

206+
TORRENT_TEST(invalid_path1)
207+
{
208+
file_storage st;
209+
#ifdef TORRENT_WINDOWS
210+
st.add_file_borrow({}, R"(+\\\()", 10);
211+
#else
212+
st.add_file_borrow({}, "+///(", 10);
213+
#endif
214+
215+
TEST_EQUAL(st.file_name(file_index_t{0}), "(");
216+
TEST_EQUAL(st.file_path(file_index_t{0}, ""), combine_path("+", "("));
217+
}
218+
219+
TORRENT_TEST(invalid_path2)
220+
{
221+
file_storage st;
222+
#ifdef TORRENT_WINDOWS
223+
st.add_file_borrow({}, R"(+\\\+\\()", 10);
224+
#else
225+
st.add_file_borrow({}, "+///+//(", 10);
226+
#endif
227+
228+
TEST_EQUAL(st.file_name(file_index_t{0}), "(");
229+
TEST_EQUAL(st.file_path(file_index_t{0}, ""), combine_path("+", combine_path("+", "(")));
230+
}
231+
205232
TORRENT_TEST(map_file)
206233
{
207234
// test map_file

0 commit comments

Comments
 (0)