Clear process lists in info before free (#2194)

This avoids a use-after-free bug because info kept pointing to
deallocated data when conky would restart.

Signed-off-by: Tin Švagelj <tin.svagelj@live.com>
Co-authored-by: EtienneLeFou <no-reply@github.com>
This commit is contained in:
Tin Švagelj
2025-05-05 23:09:06 +02:00
committed by GitHub
parent 5125cbc2f5
commit 5665e0d9f1

View File

@@ -30,6 +30,8 @@
#include "top.h"
#include <cstring>
#include "../logging.h"
#include "../prioqueue.h"
@@ -103,6 +105,14 @@ static void unhash_all_processes() {
struct process *get_first_process() { return first_process; }
void free_all_processes() {
// Before freeing all the things, we need to clear globals pointing 'em.
std::memset(info.cpu, 0, sizeof(info.cpu));
std::memset(info.memu, 0, sizeof(info.memu));
std::memset(info.time, 0, sizeof(info.time));
#ifdef BUILD_IOSTATS
std::memset(info.io, 0, sizeof(info.io));
#endif
struct process *next = nullptr, *pr = first_process;
while (pr != nullptr) {
@@ -125,9 +135,7 @@ struct process *get_process_by_name(std::string_view name) {
// Try matching against the full command line first.
if (p->name != nullptr && name == p->name) { return p; }
// If matching against full command line fails, fall back to the basename.
if (p->basename != nullptr && name == p->basename) {
return p;
}
if (p->basename != nullptr && name == p->basename) { return p; }
p = p->next;
}