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

Avoid overflow in elapsed time display #977

Merged
merged 3 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions Process.c
Expand Up @@ -211,7 +211,12 @@ void Process_printTime(RichString* str, unsigned long long totalHundredths, bool
int years = days / 365;
int daysLeft = days - 365 * years;

if (daysLeft >= 100) {
if (years >= 10000000) {
RichString_appendnAscii(str, yearColor, "eternity ", 9);
} else if (years >= 1000) {
len = xSnprintf(buffer, sizeof(buffer), "%7dy ", years);
RichString_appendnAscii(str, yearColor, buffer, len);
} else if (daysLeft >= 100) {
len = xSnprintf(buffer, sizeof(buffer), "%3dy", years);
RichString_appendnAscii(str, yearColor, buffer, len);
len = xSnprintf(buffer, sizeof(buffer), "%3dd ", daysLeft);
Expand Down Expand Up @@ -871,7 +876,15 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
Process_printLeftAlignedField(str, attr, cwd, 25);
return;
}
case ELAPSED: Process_printTime(str, /* convert to hundreds of a second */ this->processList->realtimeMs / 10 - 100 * this->starttime_ctime, coloring); return;
case ELAPSED: {
const uint64_t rt = this->processList->realtimeMs;
const uint64_t st = this->starttime_ctime * 1000;
const uint64_t dt =
rt < st ? 0 :
rt - st;
Process_printTime(str, /* convert to hundreds of a second */ dt / 10, coloring);
return;
}
case MAJFLT: Process_printCount(str, this->majflt, coloring); return;
case MINFLT: Process_printCount(str, this->minflt, coloring); return;
case M_RESIDENT: Process_printKBytes(str, this->m_resident, coloring); return;
Expand Down
3 changes: 3 additions & 0 deletions freebsd/FreeBSDProcessList.c
Expand Up @@ -509,6 +509,9 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
proc->pgrp = kproc->ki_pgid;
proc->st_uid = kproc->ki_uid;
proc->starttime_ctime = kproc->ki_start.tv_sec;
if (proc->starttime_ctime < 0) {
proc->starttime_ctime = super->realtimeMs / 1000;
}
Process_fillStarttimeBuffer(proc);
proc->user = UsersTable_getRef(super->usersTable, proc->st_uid);
ProcessList_add(super, proc);
Expand Down