summaryrefslogtreecommitdiffstats
path: root/linux
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2021-03-28 18:10:13 +0200
committerBenBE <BenBE@geshi.org>2021-03-28 19:20:28 +0200
commit73f5ecf5289b4a6431d9cc945f523b36a5996a79 (patch)
tree9db05efb8da049ece6e4924c0c96fdee73903451 /linux
parent272e72680b84a52183d39a519d6704324063bae1 (diff)
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
Diffstat (limited to 'linux')
-rw-r--r--linux/Platform.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/linux/Platform.c b/linux/Platform.c
index 7d412bc7..d5fd45dc 100644
--- a/linux/Platform.c
+++ b/linux/Platform.c
@@ -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() {

© 2014-2024 Faster IT GmbH | imprint | privacy policy