From 024893002c6d4d6358cb27aedb8869485a6218ca Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Tue, 6 Feb 2024 23:15:52 +0800 Subject: [PATCH] findTopCellItem() handle underflow case --- Meter.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Meter.c b/Meter.c index d05b7319c..8fe22a805 100644 --- a/Meter.c +++ b/Meter.c @@ -408,11 +408,17 @@ static uint8_t GraphMeterMode_findTopCellItem(const Meter* this, double scaledTo double topPoint = (valueSum / scaledTotal) * (double)(int)graphHeight; double area = (value / scaledTotal) * (double)(int)graphHeight; - assert(area >= 0.0); // "area" can be 0.0 when the division underflows - if (topPoint - (double)(int)topCell > 0.0) { - if (area > topPoint - (double)(int)topCell) + if (topPoint - (double)(int)topCell > 0.0 || (topPoint <= 0.0 && topCell < 1)) { + // If the division underflows (topPoint <= 0.0 && topCell < 1), that + // item still counts as part of the top cell ("area" would be 0.0 in + // this case). + + // Cut the excess area + if (area > topPoint - (double)(int)topCell) { area = topPoint - (double)(int)topCell; + } + assert(area >= 0.0); // Find the item that occupies the largest area of the top cell. // Favor item with higher index in case of a tie.