From b17743ff6a5b54b7b6c4cc38218289deb8bdb202 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 5 Jul 2026 10:10:10 +0200 Subject: [PATCH] network: treat negative/failed link speed as 0 /sys/class/net//speed reports -1 with no carrier. Reading it into a uint32_t wrapped to 4294967295 (without setting failbit), so {linkSpeed} showed an absurd value. Read into int64_t, check fail(), and treat negative or failed reads as 0. --- src/modules/network.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 7b4dc15f..72a367fc 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -90,13 +90,16 @@ uint32_t waybar::modules::Network::readLinkSpeed() const { if (!sysfs_speed) return 0; - uint32_t speed; + // Read into a signed type: /sys/class/net//speed reports -1 when there is + // no carrier. Extracting -1 into an unsigned type would wrap to a huge value + // (and would not set failbit), so use a signed type and validate the result. + int64_t speed = 0; sysfs_speed >> speed; - if (sysfs_speed.bad()) // read fails on incompatible devices + if (sysfs_speed.fail() || speed < 0) // read fails on incompatible devices return 0; - return speed; + return static_cast(speed); } waybar::modules::Network::Network(const std::string& id, const Json::Value& config)