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>
This commit is contained in:
Old-Ding 2026-07-06 06:45:39 +08:00 committed by Xiang Xiao
parent da3234f936
commit 6eead56aa9
2 changed files with 12 additions and 4 deletions

View file

@ -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;
}

View file

@ -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);
}