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

Use size_t for Process offset values #1588

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Explorer09
Copy link
Contributor

Convert cmdlineBasenameStart, cmdlineBasenameEnd and procExeBasenameOffset values in Process to size_t type. Improve functions that search for "basenames", "comm" and "procExe" to use size_t for offsets as well.

I made these set of code changes before the length properties of RichString object can be upgraded to size_t.

Note: This PR contains many platform specific code changes. I didn't test all of them. It is possible that I broke something.

@Explorer09 Explorer09 force-pushed the process-offsets-size-type branch from 454cf1e to 87ab00e Compare January 15, 2025 21:17
@@ -1057,7 +1057,7 @@ void Process_updateExe(Process* this, const char* exe) {
if (exe) {
this->procExe = xStrdup(exe);
const char* lastSlash = strrchr(exe, '/');
this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (size_t)(lastSlash - exe + 1) : 0;
this->procExeBasenameOffset = (lastSlash > exe && *(lastSlash + 1) != '\0') ? (size_t)(lastSlash - exe + 1) : 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Side note: I have no idea why this conditional needs a lastSlash != exe. I thought it should be legitimate when lastSlash == exe, that is, when the exe string begins with a slash such as "/vmlinuz".

@BenBE BenBE requested review from natoscott and BenBE January 16, 2025 06:53
@BenBE BenBE added enhancement Extension or improvement to existing feature code quality ♻️ Code quality enhancement Linux 🐧 Linux related issues FreeBSD 👹 FreeBSD related issues MacOS 🍏 MacOS / Darwin related issues BSD 🐡 Issues related to *BSD PCP PCP related issues NetBSD 🎏 NetBSD related issues OpenBSD 🐡 OpenBSD related issues DragonflyBSD 🪰 DragonflyBSD related issues labels Jan 16, 2025
@Explorer09 Explorer09 marked this pull request as draft January 16, 2025 11:41
@Explorer09
Copy link
Contributor Author

Well, it looks like the changes to size_t do create an undefined behavior.

The commStart and commEnd variables in Process_makeCommandStr(), they can become negative during the stripExeFromCmdline code path.

I think the logic there needs to be revised. While I could workaround by using ssize_t for commStart and commEnd, that doesn't look like a long term solution.

I would try to make a commit that properly fixes that.

Improve readability of local variables.

Signed-off-by: Kang-Che Sung <[email protected]>
The matchCmdlinePrefixWithExeSuffix() internal function (in Process.c)
can match basenames in multiple tries in case the
"cmdlineBasenameStart" input value is unreliable. Take advantage of
this and update "cmdlineBasenameStart" with the new offset detected by
the function.

Also make the matching behavior consistent regardless of
"showMergedCommand" setting.

Signed-off-by: Kang-Che Sung <[email protected]>
@Explorer09 Explorer09 force-pushed the process-offsets-size-type branch from 87ab00e to c0c9a0b Compare January 16, 2025 22:21
Add a special case when the process "comm" string is found in
"cmdline", but at the starting basename position that would be stripped
by "stripExeFromCmdline" setting. This special case will now highlight
the basename of "exe" string as well as the first token of "cmdline"
(as the two strings are concatenated together when displaying).

Signed-off-by: Kang-Che Sung <[email protected]>
Convert the members "cmdlineBasenameStart", "cmdlineBasenameEnd" and
"procExeBasenameOffset" in "Process" to size_t type. Also upgrade many
routines that search for "basenames", COMM, and PROC_EXE string to use
size_t for iterators.

The "cmdlineBasenameEnd" value is no longer allowed to negative. It is
now set to 0 during Process_init().

Signed-off-by: Kang-Che Sung <[email protected]>
@Explorer09 Explorer09 force-pushed the process-offsets-size-type branch from c0c9a0b to 707cde1 Compare January 16, 2025 22:44
@BenBE
Copy link
Member

BenBE commented Jan 16, 2025

FYI; the last time I needed to work on this logic, it took me the better of 3 months to track down some issue with the command line handling in this code. So be careful with your changes; the details are important with this part of the code …

@Explorer09 Explorer09 marked this pull request as ready for review January 16, 2025 23:39
@Explorer09
Copy link
Contributor Author

@BenBE
The changes of upgrading the length values of RichString to size_t is ready here:
https://github.com/Explorer09/htop-1/commits/rich-string-chlen/

But I'm not sure I should file a new PR for it or just merge with this one. The new changes depend on this PR and I've seen you added tags and categories to this PR already.

@BenBE
Copy link
Member

BenBE commented Jan 19, 2025

@Explorer09 Make a second PR as this makes discussing the changes easier. From a quick view at your commits on that branch I already noticed some things I'd like to change with that patchset …

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BSD 🐡 Issues related to *BSD code quality ♻️ Code quality enhancement DragonflyBSD 🪰 DragonflyBSD related issues enhancement Extension or improvement to existing feature FreeBSD 👹 FreeBSD related issues Linux 🐧 Linux related issues MacOS 🍏 MacOS / Darwin related issues NetBSD 🎏 NetBSD related issues OpenBSD 🐡 OpenBSD related issues PCP PCP related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants