w00t-labs / libtorrent

Automatically exported from code.google.com/p/libtorrent
Other
0 stars 0 forks source link

Segmentation fault while checking add_torrent_alert error condition (in libtorrent_aio branch) #274

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Testing this code I got an issue (segmentation fault) when reading error 
message from add_torrent_alert event.

if (add_torrent_alert *p = alert_cast<add_torrent_alert>(a))
{
  if (p->error)
    printf("error add torrent %s\n", p->error.message().c_str());
  else
  {
    torrent_handle h = p->handle;
    //usefull handler here
  }
}

I could check that almost all add_torrent_alerts received have the p->error 
defined (ie, with some error being reported)
But, in fact, there's no error message, then the client code makes a segfault.

Checking the libtorrent files include/libtorrent/alert_types.hpp and 
src/alerts.cpp , i made some changes which solved this error issue.

In src/alerts.cpp

-       std::string add_torrent_alert::message() const
-       {
-               char msg[600];
-               if (error)
-               {
-                       snprintf(msg, sizeof(msg), "failed to add torrent: %s", 
error.message().c_str());
-               }
-               else
-               {
-                       snprintf(msg, sizeof(msg), "added torrent: %s", 
!params.url.empty() ? params.url.c_str() : params.ti->name().c_str());
-               }

In include/libtorrent/alerts_type.hpp

-               add_torrent_alert(torrent_handle h, add_torrent_params const& 
p, error_code ec)
+               add_torrent_alert(torrent_handle h, add_torrent_params const& 
p, error_code const& ec)
                        : torrent_alert(h)
                        , params(p)
                        , error(ec)
                {}

                TORRENT_DEFINE_ALERT(add_torrent_alert);

                const static int static_category = alert::status_notification;
-               virtual std::string message() const;
+               virtual std::string message() const
+               {
+                       if (error)
+                               return "failed to add torrent: " + 
error.message();
+                       else
+                               return "added torrent: " + (!params.url.empty() 
? params.url : params.ti->name());
+               }

After this, the issue of p->error disappeared and add_torrent_alert handler now 
works right!

Original issue reported on code.google.com by mul...@gmail.com on 30 Nov 2011 at 6:47

GoogleCodeExporter commented 8 years ago
it looks like this might be you linking against a different boost-system 
library than libtorrent links against. I'm assuming the relevant change is 
moving interpreting the error_code into the header (using your version of 
boost-system) instead of leaving it in alert.cpp, using libtorrent's 
boost-system.

Original comment by arvid.no...@gmail.com on 1 Dec 2011 at 3:02

GoogleCodeExporter commented 8 years ago
i'm linking against libboost_system-mt.so.1.47.0

yep, only changing to const& fix the problem.

i'm also experiencing problems with the state_update_alert
which i can read the state_update_alert->message() but my if handler does not 
catch the alert!

such as:
-----------
printf("alert message is %s\n", a->message()); // ok here

if (state_update_alert *su = alert_cast<state_update_alert>(a))
{
... // never called
}
-----

I'm trying to fix this issue (against boost 1.47.0) by explicitly defining the 
constructor of state_update_alert() which is not defined yet.

If i get some good point, i'll post here. 

Original comment by mul...@gmail.com on 1 Dec 2011 at 3:25

GoogleCodeExporter commented 8 years ago
it worked :)

at alert_tyoes.hpp

        struct TORRENT_EXPORT state_update_alert : alert
        {
+                state_update_alert() {}

                TORRENT_DEFINE_ALERT(state_update_alert);

                const static int static_category = alert::status_notification;
                virtual std::string message() const;
                virtual bool discardable() const { return false; }

                std::vector<torrent_status> status;
        };

Original comment by mul...@gmail.com on 1 Dec 2011 at 3:48