From cb7bb52551602433582b50ea19f75e50e3e5b832 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 2 Aug 2023 06:51:55 -0700 Subject: [PATCH] uptime: fix Y5881633 bug * 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 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/uptime.c b/src/uptime.c index b9d5e3c02..bdbf1451a 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -47,11 +47,11 @@ 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); -- 2.11.4.GIT