]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
- handle large values correctly
authormlichvar <mlichvar>
Thu, 12 Oct 2006 13:48:07 +0000 (13:48 +0000)
committermlichvar <mlichvar>
Thu, 12 Oct 2006 13:48:07 +0000 (13:48 +0000)
scale.c

diff --git a/scale.c b/scale.c
index f898916b339327b8b18c1931dfd2441bd2833b7e..2667af0c3be069a60061e94997cb5e7fa0773625 100644 (file)
--- a/scale.c
+++ b/scale.c
@@ -48,11 +48,17 @@ void newtScaleSet(newtComponent co, unsigned long long amount) {
     struct scale * sc = co->data;
     int newPercentage;
 
-    sc->charsSet = (amount * co->width) / sc->fullValue;
-    newPercentage = (amount * 100) / sc->fullValue;
-
-    if (newPercentage > 100)
+    if (amount >= sc->fullValue) {
        newPercentage = 100;
+       sc->charsSet = co->width;
+    } else if (sc->fullValue >= -1ULL / (100 > co->width ? 100 : co->width)) {
+       /* avoid overflow on large numbers */
+       sc->charsSet = amount / (sc->fullValue / co->width);
+       newPercentage = amount / (sc->fullValue / 100);
+    } else {
+       sc->charsSet = (amount * co->width) / sc->fullValue;
+       newPercentage = (amount * 100) / sc->fullValue;
+    }
     
     if (newPercentage != sc->percentage) {
        sc->percentage = newPercentage;