Skip to content

Commit

Permalink
validate add_torrent_params::save_path at run-time
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jan 25, 2025
1 parent 5ba1f46 commit 4401e98
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

2.0.11 released

* validate add_torrent_params::save_path at run-time
* fix race condition when cancelling requests after becoming a seed
* fix performance bug in the file pool, evicting MRU instead of LRU (HanabishiRecca)
* fix bug where file_progress could sometimes be reported as >100%
Expand Down
2 changes: 2 additions & 0 deletions include/libtorrent/error_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ namespace errors {
// specifying the flag to only save when there's anything new to save
// (torrent_handle::only_if_modified) and there wasn't anything changed.
resume_data_not_modified,
// the save_path in add_torrent_params is not valid
invalid_save_path,


// The HTTP header was not correctly formatted
Expand Down
2 changes: 1 addition & 1 deletion src/error_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ namespace libtorrent {
"invalid piece index in slot list",
"pieces needs to be reordered",
"fastresume not modified since last save",
"",
"invalid save_path",
"",
"",
"",
Expand Down
18 changes: 18 additions & 0 deletions src/session_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,12 @@ namespace {
#ifndef BOOST_NO_EXCEPTIONS
torrent_handle session_handle::add_torrent(add_torrent_params&& params)
{
#ifndef BOOST_NO_EXCEPTIONS
if (params.save_path.empty())
aux::throw_ex<system_error>(error_code(errors::invalid_save_path));
#else
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
#endif

#if TORRENT_ABI_VERSION < 3
if (!params.info_hashes.has_v1() && !params.info_hashes.has_v2() && !params.ti)
Expand Down Expand Up @@ -435,7 +440,12 @@ namespace {

torrent_handle session_handle::add_torrent(add_torrent_params&& params, error_code& ec)
{
#ifndef BOOST_NO_EXCEPTIONS
if (params.save_path.empty())
aux::throw_ex<system_error>(error_code(errors::invalid_save_path));
#else
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
#endif

#if TORRENT_ABI_VERSION < 3
if (!params.info_hashes.has_v1() && !params.info_hashes.has_v2() && !params.ti)
Expand Down Expand Up @@ -467,7 +477,12 @@ namespace {

void session_handle::async_add_torrent(add_torrent_params&& params)
{
#ifndef BOOST_NO_EXCEPTIONS
if (params.save_path.empty())
aux::throw_ex<system_error>(error_code(errors::invalid_save_path));
#else
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
#endif

#if TORRENT_ABI_VERSION < 3
if (!params.info_hashes.has_v1() && !params.info_hashes.has_v2() && !params.ti)
Expand Down Expand Up @@ -530,6 +545,9 @@ namespace {
{
TORRENT_ASSERT_PRECOND(!save_path.empty());

if (save_path.empty())
aux::throw_ex<system_error>(error_code(errors::invalid_save_path));

add_torrent_params p;
p.trackers.push_back(tracker_url);
p.info_hashes.v1 = info_hash;
Expand Down
13 changes: 13 additions & 0 deletions test/test_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ TORRENT_TEST(async_add_torrent_deprecated_magnet)
}
#endif

TORRENT_TEST(async_add_torrent_no_save_path)
{
settings_pack p = settings();
p.set_int(settings_pack::alert_mask, ~0);
lt::session ses(p);

add_torrent_params atp;
atp.info_hashes.v1.assign("abababababababababab");
atp.save_path = "";
TEST_THROW(ses.add_torrent(atp));
TEST_THROW(ses.async_add_torrent(atp));
}

TORRENT_TEST(async_add_torrent_duplicate_error)
{
settings_pack p = settings();
Expand Down

0 comments on commit 4401e98

Please sign in to comment.