From 075845ca43ce116b21b65af6be04090a8ed9b557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Sun, 12 Jan 2025 19:56:55 +0100 Subject: [PATCH] Do fscanf error handling --- .../modules/unsuck/unsuck_platform_specific.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Converter/modules/unsuck/unsuck_platform_specific.cpp b/Converter/modules/unsuck/unsuck_platform_specific.cpp index 0fc6b148..77ea47da 100644 --- a/Converter/modules/unsuck/unsuck_platform_specific.cpp +++ b/Converter/modules/unsuck/unsuck_platform_specific.cpp @@ -306,7 +306,10 @@ void init() { numProcessors = std::thread::hardware_concurrency(); FILE* file = fopen("/proc/stat", "r"); - fscanf(file, "cpu %llu %llu %llu %llu", &lastTotalUser, &lastTotalUserLow, &lastTotalSys, &lastTotalIdle); + int ret = fscanf(file, "cpu %llu %llu %llu %llu", &lastTotalUser, &lastTotalUserLow, &lastTotalSys, &lastTotalIdle); + if (ret == EOF) { + perror("Could not read /proc/stat"); + } fclose(file); initialized = true; @@ -318,7 +321,14 @@ double getCpuUsage(){ unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total; file = fopen("/proc/stat", "r"); - fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow, &totalSys, &totalIdle); + int ret = fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow, &totalSys, &totalIdle); + if (ret == EOF) { + perror("Could not read /proc/stat"); + return 0.0; + } + if (ret < 4) { + return 0.0; + } fclose(file); if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow ||