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:
Christian Göttsche 2021-03-28 18:10:13 +02:00 committed by BenBE
parent 272e72680b
commit 73f5ecf528
1 changed files with 18 additions and 12 deletions

View File

@ -209,19 +209,25 @@ int Platform_getUptime() {
}
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");
if (fd) {
int total = fscanf(fd, "%32lf %32lf %32lf %32d/%32d %32d", one, five, fifteen,
&activeProcs, &totalProcs, &lastProc);
(void) total;
assert(total == 6);
fclose(fd);
}
if (!fd)
goto err;
double scanOne, scanFive, scanFifteen;
int r = fscanf(fd, "%lf %lf %lf", &scanOne, &scanFive, &scanFifteen);
fclose(fd);
if (r != 3)
goto err;
*one = scanOne;
*five = scanFive;
*fifteen = scanFifteen;
return;
err:
*one = NAN;
*five = NAN;
*fifteen = NAN;
}
int Platform_getMaxPid() {