From 71882473d8d7e45133d5c0d1e8baa32bb8dd9a57 Mon Sep 17 00:00:00 2001 From: Mark Horvath Date: Tue, 29 Apr 2025 10:55:43 +0200 Subject: [PATCH] Fix error handling of getloadavg getloadavg returns with -1 if cannot obtain load average, but the current source casts the return value to size_t right away. The cast result for such a case is probably maximum unsigned long int, so the resizing of the res vector is certainly going to fail. This change keeps the original return type of getloadavg and casts it just before the resizing of the res vector. --- src/sysinfo.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sysinfo.cc b/src/sysinfo.cc index c938b360f8..bf2fee85fc 100644 --- a/src/sysinfo.cc +++ b/src/sysinfo.cc @@ -850,11 +850,11 @@ std::vector GetLoadAvg() { !(defined(__ANDROID__) && __ANDROID_API__ < 29) static constexpr int kMaxSamples = 3; std::vector res(kMaxSamples, 0.0); - const size_t nelem = static_cast(getloadavg(res.data(), kMaxSamples)); + const auto nelem = getloadavg(res.data(), kMaxSamples); if (nelem < 1) { res.clear(); } else { - res.resize(nelem); + res.resize(static_cast(nelem)); } return res; #else