]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: fix uint64_t overflow
authorKarel Zak <kzak@redhat.com>
Fri, 17 Apr 2020 08:21:56 +0000 (10:21 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 17 Apr 2020 08:25:17 +0000 (10:25 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/998
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/strutils.c
tests/expected/misc/strtosize

index ccf48919bdc1fb82e7bdba1e08cdbe4f79071989..b76ab995205acdff60ebe33a06f243f7b5aff037 100644 (file)
@@ -603,7 +603,10 @@ char *size_to_human_string(int options, uint64_t bytes)
        /* round */
        if (frac) {
                /* get 3 digits after decimal point */
-               frac = (frac * 1000) / (1ULL << exp);
+               if (frac >= UINT64_MAX / 1000)
+                       frac = ((frac / 1024) * 1000) / (1ULL << (exp - 10)) ;
+               else
+                       frac = (frac * 1000) / (1ULL << (exp)) ;
 
                if (options & SIZE_DECIMAL_2DIGITS) {
                        /* round 4/5 and keep 2 digits after decimal point */
index abda45a57588b5cc0c75a1b9657426fa1bf4ec63..0f912f7229dd9dc8ffe04dee0978635b9e6c923c 100644 (file)
@@ -1,7 +1,7 @@
                         0 :                    0 :       0B :          0 B :           0 B
                         1 :                    1 :       1B :          1 B :           1 B
                       123 :                  123 :     123B :        123 B :         123 B
-     18446744073709551615 : 18446744073709551615 :      15E :       15 EiB :     15.01 EiB
+     18446744073709551615 : 18446744073709551615 :      16E :       16 EiB :        16 EiB
                        1K :                 1024 :       1K :        1 KiB :         1 KiB
                      1KiB :                 1024 :       1K :        1 KiB :         1 KiB
                        1M :              1048576 :       1M :        1 MiB :         1 MiB