Tracker: add some time_t overflow checks in StatusWindow

Don't pretend file copies will end in 1901, that's obviously wrong.

Helps with #11176, but we should really not use time_t here, or make it
64bit.

Fun fact: we're now closer to the end of the UNIX epoch than to the creation
of Haiku!

Change-Id: I64acc5ab29fb778fe3034c65b5a8418951d30505
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2608
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Adrien Destugues
2020-05-08 22:07:08 +00:00
committed by waddlesplash
parent d8d403ef5d
commit 9670db20dd
+10 -5
View File
@@ -794,9 +794,15 @@ BStatusView::_TimeStatusString(float availableSpace, float* _width)
double secondsRemaining = (fTotalSize - fSizeProcessed) double secondsRemaining = (fTotalSize - fSizeProcessed)
/ totalBytesPerSecond; / totalBytesPerSecond;
time_t now = (time_t)real_time_clock(); time_t now = (time_t)real_time_clock();
BString string;
if (secondsRemaining < 0 || (sizeof(time_t) == 4
&& now + secondsRemaining > INT32_MAX)) {
string = B_TRANSLATE("Finish: after several years");
} else {
char timeText[32];
time_t finishTime = (time_t)(now + secondsRemaining); time_t finishTime = (time_t)(now + secondsRemaining);
char timeText[32];
if (finishTime - now > kSecondsPerDay) { if (finishTime - now > kSecondsPerDay) {
BDateTimeFormat().Format(timeText, sizeof(timeText), finishTime, BDateTimeFormat().Format(timeText, sizeof(timeText), finishTime,
B_MEDIUM_DATE_FORMAT, B_MEDIUM_TIME_FORMAT); B_MEDIUM_DATE_FORMAT, B_MEDIUM_TIME_FORMAT);
@@ -804,16 +810,15 @@ BStatusView::_TimeStatusString(float availableSpace, float* _width)
BTimeFormat().Format(timeText, sizeof(timeText), finishTime, BTimeFormat().Format(timeText, sizeof(timeText), finishTime,
B_MEDIUM_TIME_FORMAT); B_MEDIUM_TIME_FORMAT);
} }
string = _FullTimeRemainingString(now, finishTime, timeText);
BString string(_FullTimeRemainingString(now, finishTime, timeText));
float width = StringWidth(string.String()); float width = StringWidth(string.String());
if (width > availableSpace) { if (width > availableSpace) {
string.SetTo(_ShortTimeRemainingString(timeText)); string.SetTo(_ShortTimeRemainingString(timeText));
width = StringWidth(string.String()); }
} }
if (_width != NULL) if (_width != NULL)
*_width = width; *_width = StringWidth(string.String());
return string; return string;
} }