Fix divide-by-zero crash

Previously here was using a cheap method to avoid divisor becoming < 0, but from
the crash stacktrace it seems this is not enough, now the divisor is properly
clamped to have 1 as the minimum.
Also it will now display "Unknown" for invalid calculation results.

Closes #9857.
This commit is contained in:
Chocobo1
2018-11-17 19:29:51 +08:00
parent 9090f2d321
commit d866033a8e
2 changed files with 10 additions and 6 deletions

View File

@@ -252,9 +252,11 @@ void TorrentsController::propertiesAction()
dataDict[KEY_PROP_UPLOADED] = torrent->totalUpload();
dataDict[KEY_PROP_UPLOADED_SESSION] = torrent->totalPayloadUpload();
dataDict[KEY_PROP_DL_SPEED] = torrent->downloadPayloadRate();
dataDict[KEY_PROP_DL_SPEED_AVG] = torrent->totalDownload() / (1 + torrent->activeTime() - torrent->finishedTime());
const int dlDuration = torrent->activeTime() - torrent->finishedTime();
dataDict[KEY_PROP_DL_SPEED_AVG] = torrent->totalDownload() / ((dlDuration == 0) ? -1 : dlDuration);
dataDict[KEY_PROP_UP_SPEED] = torrent->uploadPayloadRate();
dataDict[KEY_PROP_UP_SPEED_AVG] = torrent->totalUpload() / (1 + torrent->activeTime());
const int ulDuration = torrent->activeTime();
dataDict[KEY_PROP_UP_SPEED_AVG] = torrent->totalUpload() / ((ulDuration == 0) ? -1 : ulDuration);
dataDict[KEY_PROP_DL_LIMIT] = torrent->downloadLimit() <= 0 ? -1 : torrent->downloadLimit();
dataDict[KEY_PROP_UP_LIMIT] = torrent->uploadLimit() <= 0 ? -1 : torrent->uploadLimit();
dataDict[KEY_PROP_WASTED] = torrent->wastedSize();