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

Show year as start time for processes older than a year #1116

Merged
merged 2 commits into from
Jul 31, 2023

Conversation

arza-zara
Copy link
Contributor

Old format is month+day which can be ambiguous.

Not sure what to do with this compilation warning:

Process.c: In function ‘Process_fillStarttimeBuffer’:                                                                                                    
Process.c:288:4: warning: format not a string literal, format string not checked [-Wformat-nonliteral]
  288 |    strftime(this->starttime_show, sizeof(this->starttime_show) - 1, fmt, &date);

@BenBE BenBE added the enhancement Extension or improvement to existing feature label Oct 21, 2022
@BenBE
Copy link
Member

BenBE commented Oct 21, 2022

That warning says, that when calling strftime you must provide a string literal as part of the call itself instead of providing it through a variable. This is to protect against a class of format string vulnerabilities where the format string might be under control of an attacker. In this case the warning is a false positive, but nonetheless it's sound advise to have this argument always be strictly a literal or an expression yielding a literal (as is done in the old version.

On the actual change: I see the problem you're addressing but I'm not sure the current suggested patch is a sensible way to go forward with it.

@Explorer09
Copy link
Contributor

Just a note: I tried this workaround in Compiler Explorer but it results in a slightly larger code (three assembly call instructions instead of merged to one).
I think we are better ignore that warning now, or report to the compiler developers that this looks like a bug (false positive).

   if (this->starttime_ctime > now - 86400) {
      strftime(this->starttime_show, sizeof(this->starttime_show) - 1, "%R ", &date);
   } else if (this->starttime_ctime > now - 364*86400) {
      strftime(this->starttime_show, sizeof(this->starttime_show) - 1, "%b%d ", &date);
   } else {
      strftime(this->starttime_show, sizeof(this->starttime_show) - 1, " %Y ", &date);
   }

@cgzones
Copy link
Member

cgzones commented Oct 21, 2022

Suggestion to please the compiler:

void Process_fillStarttimeBuffer(Process* this) {
   struct tm date;
   time_t now = time(NULL);
   (void) localtime_r(&this->starttime_ctime, &date);

   strftime(this->starttime_show,
            sizeof(this->starttime_show) - 1,
            (this->starttime_ctime > now - 86400) ? "%R " : (this->starttime_ctime > now - 364*86400) ? "%b%d " : " %Y ",
            &date);
}

@BenBE
Copy link
Member

BenBE commented Feb 4, 2023

@natoscott Can you take a look at this patch re PCP time archive support? Not sure if time(NULL) is acceptable for when replaying old PCP archives.

@BenBE BenBE added this to the 3.3.0 milestone Feb 4, 2023
Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even the the code might be a bit larger (we are not constrained on this), I'd prefer the fully static format string variant here. And be it to avoid accidentally providing the wrong number of arguments and thus causing information leakage bugs …

Process.c Show resolved Hide resolved
@cgzones cgzones requested a review from BenBE June 1, 2023 21:57
Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to avoid confusion regarding the year display the switch-over should be similar to what is used for things like ls, where files older than like 6 months are marked by the year, instead of a precise date/time.

Otherwise LGTM.

@BenBE BenBE requested a review from natoscott June 1, 2023 22:13
@natoscott
Copy link
Member

@natoscott Can you take a look at this patch re PCP time archive support? Not sure if time(NULL) is acceptable for when replaying old PCP archives.

Sorry, I missed this question earlier. We don't have archive support yet in pcp-htop but yes, this sort of thing would be problematic from that POV. In general calling time() for every process we find is not ideal anyway - it would be more efficient to use the time_t from the timestamp we take at each time step. Nowadays this lives in the 'realtime.tv_sec' field of struct Machine and using this has the added benefit that it will do the right thing with PCP archives when we get to that.

Copy link
Member

@natoscott natoscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per inline comment, it'd be slightly more efficient to use the timestamp we already took for this sample. Should be a relatively straight forward change too.
Otherwise, LGTM.

Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you moving the call to Process_fillStarttimeBuffer relative to the ProcessList_add call everywhere?

dragonflybsd/DragonFlyBSDProcessList.c Outdated Show resolved Hide resolved
@arza-zara
Copy link
Contributor Author

Why are you moving the call to Process_fillStarttimeBuffer relative to the ProcessList_add call everywhere?

Process_fillStarttimeBuffer needed processList. It's not needed anymore after rebasing on main so I'll remove that.

Copy link
Member

@natoscott natoscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@BenBE BenBE merged commit 2d4e5cb into htop-dev:main Jul 31, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Extension or improvement to existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants