mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
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:
parent
da3234f936
commit
6eead56aa9
2 changed files with 12 additions and 4 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue