htop/backport-Linux-allocate-cpuData-before-reading-cpu-count.patch
yixiangzhike ebafa107ad Backport upstream patches to fix htop crash in container
(cherry picked from commit 093f5a16ef7d960cc0af58c50a70845856b192bf)
2023-11-29 10:37:10 +08:00

51 lines
1.6 KiB
Diff

From ba4c67942c98716fcfd1fd576421a964077cefed Mon Sep 17 00:00:00 2001
From: er-azh <80633916+er-azh@users.noreply.github.com>
Date: Sat, 26 Mar 2022 17:27:36 +0430
Subject: [PATCH] Linux: allocate cpuData before reading cpu count.
---
linux/LinuxProcessList.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c
index 3002e829..67493cd0 100644
--- a/linux/LinuxProcessList.c
+++ b/linux/LinuxProcessList.c
@@ -174,16 +174,19 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) {
LinuxProcessList* this = (LinuxProcessList*) super;
unsigned int existing = 0, active = 0;
- DIR* dir = opendir("/sys/devices/system/cpu");
- if (!dir) {
+ // Initialize the cpuData array before anything else.
+ if (!this->cpuData) {
this->cpuData = xReallocArrayZero(this->cpuData, super->existingCPUs ? (super->existingCPUs + 1) : 0, 2, sizeof(CPUData));
this->cpuData[0].online = true; /* average is always "online" */
this->cpuData[1].online = true;
super->activeCPUs = 1;
super->existingCPUs = 1;
- return;
}
+ DIR* dir = opendir("/sys/devices/system/cpu");
+ if (!dir)
+ return;
+
unsigned int currExisting = super->existingCPUs;
const struct dirent* entry;
@@ -233,6 +236,10 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) {
closedir(dir);
+ // return if no CPU is found
+ if (existing < 1)
+ return;
+
#ifdef HAVE_SENSORS_SENSORS_H
/* When started with offline CPUs, libsensors does not monitor those,
* even when they become online. */
--
2.27.0