Bugfix: Inappropriate truncate if argv[0] contains a '/' #1854
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to fix the bug described in issue #1817.
To start, I want to present a small program that triggers the described issue.
Let's call it
issue1817testfile.c:The described issue occurs in the code of
LinuxProcessTable.c:When the option "Show program path" is not enabled, this program is displayed
as
"3]"inhtop, instead of showing the full text.Problem Explanation
The issue arises because we search for the
/character to split the commandline, which works fine in most cases. However, the command line in
/proc/<pid>/cmdlinecan have the following formats:/home/foobar/path/to/executable./relative/path/to/executablerelative/path/to/executableThe current implementation assumes that encountering a
/is always a validseparator for the path. This can cause problems when a program modifies
argv[0], leading to incomplete or truncated commands beingdisplayed in
htop.Proposed Fix
The most efficient approach is to filter out cases where we deal with absolute
(
/) or relative (./) paths. These are the most common command line formats.We can treat these as paths that should be split by the
/character.For case (3) where no leading
./is given (e.g.,path/to/executable),we can continue outputting the full string, acknowledging that this may occur
rarely. My argument is:
"It's better to show too much information than too little."
Alternatively, we could read the target from the symlink
/proc/<pid>/exeand compare it to the content of the first string in
/proc/<pid>/cmdline:For example, if
/proc/<pid>/exepoints to/home/foobar/path/to/exefile,we would need to locate
exefilein the first string of/proc/<pid>/cmdlineand set
tokenStartto that index (ifexefileis found in the string).However, this would only solve another special case that practically never
or extremely rarely occurs. Therefore, I have ignored these
type 3 paths ("path/to/executable") for now.
Edge Cases
There is still a small chance that a program path could sneak into the
output due to unusual cases, but this is rare. It’s important to note that even
with this fix, some edge cases might still result in full path being shown.
Therefore, the goal here is to minimize errors, but we can't guarantee 100%
coverage for all possible edge cases.
But I think my patch results in the displayed paths being more useful to the
user in individual cases than the original code.