This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 6eead56aa system: resmonitor: check CPU load reads
6eead56aa is described below
commit 6eead56aa9b164de7d362f009a9b1ed556ecc9fb
Author: Old-Ding <[email protected]>
AuthorDate: Mon Jul 6 06:45:39 2026 +0800
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 <[email protected]>
---
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);
}