{"action":"closed","issue":{"url":"https://api.github.com/repos/arvidn/libtorrent/issues/712","repository_url":"https://api.github.com/repos/arvidn/libtorrent","labels_url":"https://api.github.com/repos/arvidn/libtorrent/issues/712/labels{/name}","comments_url":"https://api.github.com/repos/arvidn/libtorrent/issues/712/comments","events_url":"https://api.github.com/repos/arvidn/libtorrent/issues/712/events","html_url":"https://github.com/arvidn/libtorrent/issues/712","id":153662198,"number":712,"title":"aux::time_now() used in comparisons with clock_type::now()","user":{"login":"jonathanmcdougall","id":19160663,"avatar_url":"https://avatars.githubusercontent.com/u/19160663?v=3","gravatar_id":"","url":"https://api.github.com/users/jonathanmcdougall","html_url":"https://github.com/jonathanmcdougall","followers_url":"https://api.github.com/users/jonathanmcdougall/followers","following_url":"https://api.github.com/users/jonathanmcdougall/following{/other_user}","gists_url":"https://api.github.com/users/jonathanmcdougall/gists{/gist_id}","starred_url":"https://api.github.com/users/jonathanmcdougall/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonathanmcdougall/subscriptions","organizations_url":"https://api.github.com/users/jonathanmcdougall/orgs","repos_url":"https://api.github.com/users/jonathanmcdougall/repos","events_url":"https://api.github.com/users/jonathanmcdougall/events{/privacy}","received_events_url":"https://api.github.com/users/jonathanmcdougall/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-05-08T15:40:37Z","updated_at":"2016-05-23T03:50:05Z","closed_at":"2016-05-23T03:50:04Z","body":"RC_1_1, windows 10 x64, visual c++ 2015 update 2\r\n\r\nI just had a division by zero on torrent.cpp:8421:\r\n\r\n    time_point now = aux::time_now();\r\n    boost::int64_t lhs_time_connected = total_seconds(now - lhs->connected_time());\r\n    boost::int64_t rhs_time_connected = total_seconds(now - rhs->connected_time());\r\n\r\n    lhs_transferred /= lhs_time_connected + 1;      // <--- here\r\n    rhs_transferred /= (rhs_time_connected + 1);\r\n\r\nTurns out `aux::time_now()` is using a \"cached time\" that's supposed to be updated every 100 ms, but this depends on the speed of the session thread. If things slow down, the cached time can be quite behind.\r\n\r\nThe problem is that there is at least one comparison between the cached time and the actual time. In the code above, the call to `peer_connection::connected_time()` returns `m_connect`, which is set in `peer_connection::start()` using `clock_type::now()`. If the session thread is running slow, this can be in the future compared to `aux::time_now()`. So `lhs_time_connected` can end up being rounded to `-1`, giving a division by zero.\r\n\r\nA quick fix for this particular crash would be to use `clock_type::time_now()` for the comparison, but I'm not sure about the impact on performance. Both `lhs_time_connected` and `rhs_time_connected` could also be clamped to 0.\r\n\r\nA more general fix would be to make sure \"cached time\" is never compared to \"actual time\", unless negative time differences are acceptable."}}
{"actor":{"display_login":"arvidn"}}