From 132e69b5807140cb826a6c6324f66760c91cf79f Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 10 Oct 2026 13:35:11 +0800 Subject: [PATCH] Percent: clamps bar width to 0% ~ 100% --- CHANGELOG.md | 3 ++- src/common/impl/percent.c | 7 +++++-- tests/percent.c | 11 +++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c3b9093a..512785657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,8 @@ Bugfixes: * Fixed `/boot` being hidden by default on Haiku, where it is the system volume. (Disk, Haiku) * Improved Snapdragon X1/X2 model name detection on Linux. (#2625, CPU, Linux) * Fixed the Android build failing with `GL/gl.h` not found, because the NDK ships the EGL headers that turned OpenGL detection on but not the desktop GL headers it includes. (OpenGL, Android) -* Fixed MAC address reporting 20:00:00:00:00:00 on macOS 27. (LocalIP, Apple) +* Fixed MAC address reporting `20:00:00:00:00:00` on macOS 27. (LocalIP, Apple) +* Fixed assertion failure when percentage exceeds 100 (Percent) Logos: * Added filled Artix logo (#2631) diff --git a/src/common/impl/percent.c b/src/common/impl/percent.c index 0b8c8407e..2858698ee 100644 --- a/src/common/impl/percent.c +++ b/src/common/impl/percent.c @@ -88,8 +88,11 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf FFPercentageTypeFlags percentType = config.type == 0 ? options->percentType : config.type; - uint8_t blocksPercent = (uint8_t) (percent / 100.0 * options->barWidth + 0.5); - assert(blocksPercent <= options->barWidth); + uint8_t blocksPercent = 0; + if (percent > 0) { + double blocks = percent / 100.0 * options->barWidth + 0.5; + blocksPercent = (uint8_t) (blocks >= options->barWidth ? options->barWidth : blocks); + } bool autoColorElapsed = ffStrbufIgnCaseEqualS(&options->barColorElapsed, "auto"); diff --git a/tests/percent.c b/tests/percent.c index e697a854d..834ad143d 100644 --- a/tests/percent.c +++ b/tests/percent.c @@ -180,6 +180,17 @@ int main(void) { VERIFY_BAR(14, config, "[ ■--------- ]"); VERIFY_BAR(15, config, "[ ■■-------- ]"); + // A value above 100 saturates the bar. That is reachable from real data: the load average is + // normalized against the number of cores, so it exceeds 100% as soon as the load does. + VERIFY_BAR(101, config, "[ ■■■■■■■■■■ ]"); + VERIFY_BAR(150, config, "[ ■■■■■■■■■■ ]"); + + // Narrowing to `uint8_t` before clamping would wrap around here and silently render 4 blocks + VERIFY_BAR(2600, config, "[ ■■■■■■■■■■ ]"); + + // A negative value fills nothing + VERIFY_BAR(-60, config, "[ ---------- ]"); + options->barWidth = 4; VERIFY_BAR(0, config, "[ ---- ]"); VERIFY_BAR(25, config, "[ ■--- ]");