]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
uptime: fix Y5881633 bug
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 2 Aug 2023 13:51:55 +0000 (06:51 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 2 Aug 2023 13:52:39 +0000 (06:52 -0700)
* src/uptime.c (print_uptime): Prefer signed types.
Fix unlikely bug on platforms with 32-bit long and 64-bit time_t
if the idle time exceeds 2**31 days (about 6 million years...).

src/uptime.c

index b9d5e3c02f8e06ae0cd30e2ba0913d2018eed034..bdbf1451a1b37826ba27a71c325399e20c071731 100644 (file)
 static void
 print_uptime (size_t n, const STRUCT_UTMP *this)
 {
-  size_t entries = 0;
+  idx_t entries = 0;
   time_t boot_time = 0;
   time_t time_now;
   time_t uptime = 0;
-  long int updays;
+  intmax_t updays;
   int uphours;
   int upmins;
   struct tm *tmn;
@@ -120,8 +120,8 @@ print_uptime (size_t n, const STRUCT_UTMP *this)
       uptime = time_now - boot_time;
     }
   updays = uptime / 86400;
-  uphours = (uptime - (updays * 86400)) / 3600;
-  upmins = (uptime - (updays * 86400) - (uphours * 3600)) / 60;
+  uphours = uptime % 86400 / 3600;
+  upmins = uptime % 86400 % 3600 / 60;
   tmn = localtime (&time_now);
   /* procps' version of uptime also prints the seconds field, but
      previous versions of coreutils don't. */
@@ -135,15 +135,15 @@ print_uptime (size_t n, const STRUCT_UTMP *this)
   else
     {
       if (0 < updays)
-        printf (ngettext ("up %ld day %2d:%02d,  ",
-                          "up %ld days %2d:%02d,  ",
+        printf (ngettext ("up %"PRIdMAX" day %2d:%02d,  ",
+                          "up %"PRIdMAX" days %2d:%02d,  ",
                           select_plural (updays)),
                 updays, uphours, upmins);
       else
         printf (_("up  %2d:%02d,  "), uphours, upmins);
     }
-  printf (ngettext ("%lu user", "%lu users", select_plural (entries)),
-          (unsigned long int) entries);
+  printf (ngettext ("%td user", "%td users", select_plural (entries)),
+          entries);
 
   loads = getloadavg (avg, 3);