mirror of https://github.com/xzeldon/htop.git
Linux: handle garbage in /proc/loadavg
When parsing the content of /proc/loadavg via fscanf(3), ensure client passed parameters are set to sanitized values. Related to: #581
This commit is contained in:
parent
272e72680b
commit
73f5ecf528
|
@ -209,19 +209,25 @@ int Platform_getUptime() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
|
void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
|
||||||
int activeProcs, totalProcs, lastProc;
|
|
||||||
*one = 0;
|
|
||||||
*five = 0;
|
|
||||||
*fifteen = 0;
|
|
||||||
|
|
||||||
FILE* fd = fopen(PROCDIR "/loadavg", "r");
|
FILE* fd = fopen(PROCDIR "/loadavg", "r");
|
||||||
if (fd) {
|
if (!fd)
|
||||||
int total = fscanf(fd, "%32lf %32lf %32lf %32d/%32d %32d", one, five, fifteen,
|
goto err;
|
||||||
&activeProcs, &totalProcs, &lastProc);
|
|
||||||
(void) total;
|
double scanOne, scanFive, scanFifteen;
|
||||||
assert(total == 6);
|
int r = fscanf(fd, "%lf %lf %lf", &scanOne, &scanFive, &scanFifteen);
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
}
|
if (r != 3)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
*one = scanOne;
|
||||||
|
*five = scanFive;
|
||||||
|
*fifteen = scanFifteen;
|
||||||
|
return;
|
||||||
|
|
||||||
|
err:
|
||||||
|
*one = NAN;
|
||||||
|
*five = NAN;
|
||||||
|
*fifteen = NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Platform_getMaxPid() {
|
int Platform_getMaxPid() {
|
||||||
|
|
Loading…
Reference in New Issue