Skip to content

Commit 9d7443f

Browse files
committed
validate add_torrent_params::save_path at run-time
1 parent 44f19a6 commit 9d7443f

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
2.0.11 released
33

4+
* validate add_torrent_params::save_path at run-time
45
* use stricter rules for what filenames are valid on Android
56
* fix applying IP filter to DHT traffic (HanabishiRecca)
67
* fix race condition when cancelling requests after becoming a seed

include/libtorrent/error_code.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ namespace errors {
389389
// specifying the flag to only save when there's anything new to save
390390
// (torrent_handle::only_if_modified) and there wasn't anything changed.
391391
resume_data_not_modified,
392+
// the save_path in add_torrent_params is not valid
393+
invalid_save_path,
392394

393395

394396
// The HTTP header was not correctly formatted

include/libtorrent/session_handle.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ namespace libtorrent {
264264
// immediately, without waiting for the torrent to add. Notification of
265265
// the torrent being added is sent as add_torrent_alert.
266266
//
267+
// The ``save_path`` field in add_torrent_params must be set to a valid
268+
// path where the files for the torrent will be saved. Even when using a
269+
// custom storage, this needs to be set to something. If the save_path
270+
// is empty, the call to add_torrent() will throw a system_error
271+
// exception.
272+
//
267273
// The overload that does not take an error_code throws an exception on
268274
// error and is not available when building without exception support.
269275
// The torrent_handle returned by add_torrent() can be used to retrieve

src/error_code.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ namespace libtorrent {
206206
"invalid piece index in slot list",
207207
"pieces needs to be reordered",
208208
"fastresume not modified since last save",
209-
"",
209+
"invalid save_path",
210210
"",
211211
"",
212212
"",

src/session_handle.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,12 @@ namespace {
405405
#ifndef BOOST_NO_EXCEPTIONS
406406
torrent_handle session_handle::add_torrent(add_torrent_params&& params)
407407
{
408+
#ifndef BOOST_NO_EXCEPTIONS
409+
if (params.save_path.empty())
410+
aux::throw_ex<system_error>(error_code(errors::invalid_save_path));
411+
#else
408412
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
413+
#endif
409414

410415
#if TORRENT_ABI_VERSION < 3
411416
if (!params.info_hashes.has_v1() && !params.info_hashes.has_v2() && !params.ti)
@@ -435,7 +440,11 @@ namespace {
435440

436441
torrent_handle session_handle::add_torrent(add_torrent_params&& params, error_code& ec)
437442
{
438-
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
443+
if (params.save_path.empty())
444+
{
445+
ec = error_code(errors::invalid_save_path);
446+
return {};
447+
}
439448

440449
#if TORRENT_ABI_VERSION < 3
441450
if (!params.info_hashes.has_v1() && !params.info_hashes.has_v2() && !params.ti)
@@ -467,7 +476,12 @@ namespace {
467476

468477
void session_handle::async_add_torrent(add_torrent_params&& params)
469478
{
479+
#ifndef BOOST_NO_EXCEPTIONS
480+
if (params.save_path.empty())
481+
aux::throw_ex<system_error>(error_code(errors::invalid_save_path));
482+
#else
470483
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
484+
#endif
471485

472486
#if TORRENT_ABI_VERSION < 3
473487
if (!params.info_hashes.has_v1() && !params.info_hashes.has_v2() && !params.ti)
@@ -528,7 +542,12 @@ namespace {
528542
, bool const add_paused
529543
, client_data_t userdata)
530544
{
545+
#ifndef BOOST_NO_EXCEPTIONS
546+
if (save_path.empty())
547+
aux::throw_ex<system_error>(error_code(errors::invalid_save_path));
548+
#else
531549
TORRENT_ASSERT_PRECOND(!save_path.empty());
550+
#endif
532551

533552
add_torrent_params p;
534553
p.trackers.push_back(tracker_url);

test/test_session.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ TORRENT_TEST(async_add_torrent_deprecated_magnet)
126126
}
127127
#endif
128128

129+
TORRENT_TEST(async_add_torrent_no_save_path)
130+
{
131+
settings_pack p = settings();
132+
p.set_int(settings_pack::alert_mask, ~0);
133+
lt::session ses(p);
134+
135+
add_torrent_params atp;
136+
atp.info_hashes.v1.assign("abababababababababab");
137+
atp.save_path = "";
138+
TEST_THROW(ses.add_torrent(atp));
139+
TEST_THROW(ses.async_add_torrent(atp));
140+
141+
lt::error_code ec;
142+
ses.add_torrent(atp, ec);
143+
TORRENT_ASSERT(ec == error_code(lt::errors::invalid_save_path));
144+
}
145+
129146
TORRENT_TEST(async_add_torrent_duplicate_error)
130147
{
131148
settings_pack p = settings();

0 commit comments

Comments
 (0)