Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arguments of dispatched functions cannot be actually moved #7873

Open
wants to merge 2 commits into
base: RC_2_0
Choose a base branch
from

Conversation

glassez
Copy link
Contributor

@glassez glassez commented Feb 15, 2025

It seems that move-only arguments were never used, so the problem went unnoticed. I discovered it when I was experimenting with boost::promise.

@glassez
Copy link
Contributor Author

glassez commented Feb 15, 2025

Oh, damn it! libtorrent still supports C++17...

@glassez glassez force-pushed the fix-forward-args branch 2 times, most recently from 40be6c3 to 3e1b699 Compare February 15, 2025 17:31
@glassez glassez marked this pull request as draft February 15, 2025 17:33
src/session_handle.cpp Fixed Show fixed Hide fixed
src/session_handle.cpp Fixed Show fixed Hide fixed
src/session_handle.cpp Fixed Show fixed Hide fixed
src/session_handle.cpp Fixed Show fixed Hide fixed
src/session_handle.cpp Fixed Show fixed Hide fixed
src/torrent_handle.cpp Fixed Show fixed Hide fixed
src/torrent_handle.cpp Fixed Show fixed Hide fixed
src/torrent_handle.cpp Fixed Show fixed Hide fixed
src/torrent_handle.cpp Fixed Show fixed Hide fixed
src/torrent_handle.cpp Fixed Show fixed Hide fixed
@glassez glassez force-pushed the fix-forward-args branch 2 times, most recently from 145f938 to 1c465b3 Compare February 15, 2025 18:35
src/session_handle.cpp Show resolved Hide resolved
src/torrent_handle.cpp Show resolved Hide resolved
src/session_handle.cpp Show resolved Hide resolved
src/session_handle.cpp Show resolved Hide resolved
src/session_handle.cpp Show resolved Hide resolved
src/torrent_handle.cpp Show resolved Hide resolved
src/torrent_handle.cpp Show resolved Hide resolved
src/torrent_handle.cpp Show resolved Hide resolved
@glassez glassez force-pushed the fix-forward-args branch 3 times, most recently from dd85995 to fceba8d Compare February 16, 2025 04:41
@@ -92,12 +93,12 @@ namespace libtorrent {
{
std::shared_ptr<session_impl> s = m_impl.lock();
if (!s) aux::throw_ex<system_error>(errors::invalid_session_handle);
dispatch(s->get_context(), [=]() mutable
dispatch(s->get_context(), std::bind([=](auto&&... args) mutable
Copy link
Contributor Author

@glassez glassez Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that trick with std::bind isn't required since c++20. It can be rewritten as the following:

dispatch(s->get_context(), [=, ...args = std::forward<Args>(a)]() mutable
{
    // do something
});

@@ -928,11 +928,12 @@ void test_fastresume(bool const test_deprecated)
p.storage_mode = storage_mode_sparse;
error_code ignore;
torrent_handle h = ses.add_torrent(std::move(p), ignore);
TEST_CHECK(exists(combine_path(p.save_path, "temporary")));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is noteworthy that this "use-after-move" bug also went unnoticed, as p was not actually moved.

@glassez glassez marked this pull request as ready for review February 16, 2025 05:00
@@ -92,12 +93,12 @@
{
std::shared_ptr<session_impl> s = m_impl.lock();
if (!s) aux::throw_ex<system_error>(errors::invalid_session_handle);
dispatch(s->get_context(), [=]() mutable
dispatch(s->get_context(), std::bind([s, f](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable args is never read.
@@ -92,12 +93,12 @@
{
std::shared_ptr<session_impl> s = m_impl.lock();
if (!s) aux::throw_ex<system_error>(errors::invalid_session_handle);
dispatch(s->get_context(), [=]() mutable
dispatch(s->get_context(), std::bind([s, f](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.
@@ -92,12 +93,12 @@
{
std::shared_ptr<session_impl> s = m_impl.lock();
if (!s) aux::throw_ex<system_error>(errors::invalid_session_handle);
dispatch(s->get_context(), [=]() mutable
dispatch(s->get_context(), std::bind([s, f](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Declaration hides parameter Note

Local variable 'args' hides a
parameter of the same name
.
@@ -154,12 +155,12 @@
bool done = false;
Ret r;
std::exception_ptr ex;
dispatch(s->get_context(), [=, &r, &done, &ex]() mutable
dispatch(s->get_context(), std::bind([s, f, &r, &done, &ex](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable args is never read.
@@ -154,12 +155,12 @@
bool done = false;
Ret r;
std::exception_ptr ex;
dispatch(s->get_context(), [=, &r, &done, &ex]() mutable
dispatch(s->get_context(), std::bind([s, f, &r, &done, &ex](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.
@@ -122,12 +123,12 @@
std::shared_ptr<torrent> t = m_torrent.lock();
if (!t) aux::throw_ex<system_error>(errors::invalid_torrent_handle);
auto& ses = static_cast<session_impl&>(t->session());
dispatch(ses.get_context(), [=,&ses] ()
dispatch(ses.get_context(), std::bind([t, f, &ses](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.
@@ -122,12 +123,12 @@
std::shared_ptr<torrent> t = m_torrent.lock();
if (!t) aux::throw_ex<system_error>(errors::invalid_torrent_handle);
auto& ses = static_cast<session_impl&>(t->session());
dispatch(ses.get_context(), [=,&ses] ()
dispatch(ses.get_context(), std::bind([t, f, &ses](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Declaration hides parameter Note

Local variable 'args' hides a
parameter of the same name
.
@@ -190,12 +191,12 @@
bool done = false;

std::exception_ptr ex;
dispatch(ses.get_context(), [=,&r,&done,&ses,&ex] ()
dispatch(ses.get_context(), std::bind([t, f, &r, &done, &ses, &ex](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable args is never read.
@@ -190,12 +191,12 @@
bool done = false;

std::exception_ptr ex;
dispatch(ses.get_context(), [=,&r,&done,&ses,&ex] ()
dispatch(ses.get_context(), std::bind([t, f, &r, &done, &ses, &ex](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.
@@ -190,12 +191,12 @@
bool done = false;

std::exception_ptr ex;
dispatch(ses.get_context(), [=,&r,&done,&ses,&ex] ()
dispatch(ses.get_context(), std::bind([t, f, &r, &done, &ses, &ex](auto&&... args) mutable

Check notice

Code scanning / CodeQL

Declaration hides parameter Note

Local variable 'args' hides a
parameter of the same name
.
@@ -92,12 +93,12 @@ namespace libtorrent {
{
std::shared_ptr<session_impl> s = m_impl.lock();
if (!s) aux::throw_ex<system_error>(errors::invalid_session_handle);
dispatch(s->get_context(), [=]() mutable
dispatch(s->get_context(), std::bind([s, f](auto&&... args) mutable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be beneficial to move function f into the lambda too if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be beneficial to move function f into the lambda too if possible.

Generally, yes. But in this case, it is intended only for pointer to member function, so moving it will not add any advantages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. So it is a pointer rather than a function object...

@@ -92,12 +93,12 @@ namespace libtorrent {
{
std::shared_ptr<session_impl> s = m_impl.lock();
if (!s) aux::throw_ex<system_error>(errors::invalid_session_handle);
dispatch(s->get_context(), [=]() mutable
dispatch(s->get_context(), std::bind([s, f](auto&&... args) mutable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not:

Suggested change
dispatch(s->get_context(), std::bind([s, f](auto&&... args) mutable
dispatch(s->get_context(), std::bind([s, f](Args&&... args) mutable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not:

It is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants