From 43ffdb0edabd44fabd044012edc6a0189b86b2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Fri, 10 Sep 2021 11:26:56 +0200 Subject: [PATCH] Linux: zero CPU data after allocation Zero all the CPU data, like totalPeriod, after its memory allocation via realloc(3). Conditional jump or move depends on uninitialised value(s) at 0x132A9B: LinuxProcessList_scanCPUTime (LinuxProcessList.c:1928) by 0x1358C3: ProcessList_goThroughEntries (LinuxProcessList.c:2079) by 0x12A79A: ProcessList_scan (ProcessList.c:627) by 0x11CA67: CommandLine_run (CommandLine.c:357) by 0x4A81E49: (below main) (libc-start.c:314) Uninitialised value was created by a heap allocation at 0x48396C5: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x12F633: xRealloc (XUtils.c:64) by 0x12F633: xReallocArray (XUtils.c:78) by 0x1325A8: LinuxProcessList_updateCPUcount (LinuxProcessList.c:207) by 0x134E0A: ProcessList_new (LinuxProcessList.c:284) by 0x11C8D0: CommandLine_run (CommandLine.c:301) by 0x4A81E49: (below main) (libc-start.c:314) --- linux/LinuxProcessList.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 046e1864..c4b7ebc7 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -167,11 +167,11 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { DIR* dir = opendir("/sys/devices/system/cpu"); if (!dir) { - super->activeCPUs = 1; - super->existingCPUs = 1; - this->cpuData = xReallocArray(this->cpuData, 2, sizeof(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; } @@ -204,10 +204,7 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { /* readdir() iterates with no specific order */ unsigned int max = MAXIMUM(existing, id + 1); if (max > currExisting) { - this->cpuData = xReallocArray(this->cpuData, max + /* aggregate */ 1, sizeof(CPUData)); - for (unsigned int j = currExisting; j < max; j++) { - this->cpuData[j].online = false; - } + this->cpuData = xReallocArrayZero(this->cpuData, currExisting ? (currExisting + 1) : 0, max + /* aggregate */ 1, sizeof(CPUData)); this->cpuData[0].online = true; /* average is always "online" */ currExisting = max; }