summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJos Dehaes <jos.dehaes@gmail.com>2024-02-02 09:28:07 +0100
committerJos Dehaes <jos.dehaes@gmail.com>2024-02-02 09:28:07 +0100
commit2b09f29a1e2ca86595e7e8023ba6041c319491f2 (patch)
tree6123f6afeaf831d6f32de0e124c75be7fe5c29a6
parentedcb68cbb91c71de6c890f9392756f1ca15955d4 (diff)
fix: don't mangle memory for zombie processes
for a zombie process, `proc_pidpath` returns 0, and nothing is written in fullname, so it's uninitialized garbage
-rw-r--r--src/osx/btop_collect.cpp12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp
index 860e457..68aa961 100644
--- a/src/osx/btop_collect.cpp
+++ b/src/osx/btop_collect.cpp
@@ -1212,10 +1212,14 @@ namespace Proc {
//? Get program name, command, username, parent pid, nice and status
if (no_cache) {
char fullname[PROC_PIDPATHINFO_MAXSIZE];
- proc_pidpath(pid, fullname, sizeof(fullname));
- const string f_name = std::string(fullname);
- size_t lastSlash = f_name.find_last_of('/');
- new_proc.name = f_name.substr(lastSlash + 1);
+ int rc = proc_pidpath(pid, fullname, sizeof(fullname));
+ string f_name = "<defunct>";
+ if (rc != 0) {
+ f_name = std::string(fullname);
+ size_t lastSlash = f_name.find_last_of('/');
+ f_name = f_name.substr(lastSlash + 1);
+ }
+ new_proc.name = f_name;
//? Get process arguments if possible, fallback to process path in case of failure
if (Shared::arg_max > 0) {
std::unique_ptr<char[]> proc_chars(new char[Shared::arg_max]);