From 7447c360ae449bc603461f578de7ff6f4c78bbd7 Mon Sep 17 00:00:00 2001 From: Old-Ding <35417409+Old-Ding@users.noreply.github.com> Date: Mon, 6 Jul 2026 06:45:39 +0800 Subject: [PATCH] system: resmonitor: check CPU load reads Keep the default zero CPU value when the procfs load file cannot be read, and trim only the newline that was actually present. This avoids parsing uninitialized stack data in fillcpu and avoids writing before the showinfo CPU buffer when fgets returns no data. Signed-off-by: Old-Ding <35417409+Old-Ding@users.noreply.github.com> --- system/resmonitor/fillcpu.c | 7 +++++-- system/resmonitor/showinfo.c | 9 +++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/system/resmonitor/fillcpu.c b/system/resmonitor/fillcpu.c index f7101a5b7..b8e187ea8 100644 --- a/system/resmonitor/fillcpu.c +++ b/system/resmonitor/fillcpu.c @@ -97,8 +97,11 @@ static float get_cpu(int pid) } char buf[8]; - fgets(buf, 8, fp); - sscanf(buf, "%f", &cpu); + if (fgets(buf, 8, fp) != NULL) + { + sscanf(buf, "%f", &cpu); + } + fclose(fp); return cpu; } diff --git a/system/resmonitor/showinfo.c b/system/resmonitor/showinfo.c index 18713bc5f..774f8fd22 100644 --- a/system/resmonitor/showinfo.c +++ b/system/resmonitor/showinfo.c @@ -131,11 +131,16 @@ static void get_cpu(int pid, char *buf) return; } - fgets(buf, 8, fp); + if (fgets(buf, 8, fp) == NULL) + { + snprintf(buf, 8, "%.1f%%", 0.0); + fclose(fp); + return; + } /* sscanf(buf, "%f", &cpu); */ - buf[strlen(buf) - 1] = '\0'; + buf[strcspn(buf, "\n")] = '\0'; fclose(fp); }