]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip: Fix size_columns() for very large values
authorPetr Machata <petrm@nvidia.com>
Mon, 27 Jun 2022 13:18:21 +0000 (15:18 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Mon, 27 Jun 2022 19:42:11 +0000 (12:42 -0700)
For values near the 64-bit boundary, the iterative application of
powi *= 10 causes powi to overflow without the termination condition of
powi >= val having ever been satisfied. Instead, when determining the
length of the number, iterate val /= 10 and terminate when it's a single
digit.

Fixes: 49437375b6c1 ("ip: dynamically size columns when printing stats")
CC: Tariq Toukan <tariqt@nvidia.com>
CC: Itay Aveksis <itayav@nvidia.com>
Signed-off-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
ip/ipaddress.c

index 17341d287629fbd6c9fcb3f6a9a89635fc9dc3b1..5a3b1cae4079fdf070beec6bb79fbd5227622c26 100644 (file)
@@ -549,7 +549,7 @@ static void print_vfinfo(FILE *fp, struct ifinfomsg *ifi, struct rtattr *vfinfo)
 void size_columns(unsigned int cols[], unsigned int n, ...)
 {
        unsigned int i, len;
-       uint64_t val, powi;
+       uint64_t val;
        va_list args;
 
        va_start(args, n);
@@ -560,7 +560,7 @@ void size_columns(unsigned int cols[], unsigned int n, ...)
                if (human_readable)
                        continue;
 
-               for (len = 1, powi = 10; powi < val; len++, powi *= 10)
+               for (len = 1; val > 9; len++, val /= 10)
                        /* nothing */;
                if (len > cols[i])
                        cols[i] = len;