bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

uptime fixes to use c_strtod, and add overflow checking


From: Paul Eggert
Subject: uptime fixes to use c_strtod, and add overflow checking
Date: Mon, 12 Jul 2004 10:56:17 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this into my private copy:

2004-07-12  Paul Eggert  <address@hidden>

        * src/uptime.c: Include c-strtod.h.
        (print_uptime): Use c_strtod instead of setlocale and sscanf.
        Use long int rather than int to count days (for 64-bit hosts),
        and check for arithmetic overflow when converting double to time_t.

Index: uptime.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/uptime.c,v
retrieving revision 1.41
diff -p -u -r1.41 uptime.c
--- uptime.c    21 Jun 2004 15:03:35 -0000      1.41
+++ uptime.c    12 Jul 2004 17:48:12 -0000
@@ -28,6 +28,7 @@
 # include <sys/sysctl.h>
 #endif
 
+#include "c-strtod.h"
 #include "error.h"
 #include "long-options.h"
 #include "quote.h"
@@ -55,7 +56,7 @@ print_uptime (int n, const STRUCT_UTMP *
   time_t boot_time = 0;
   time_t time_now;
   time_t uptime = 0;
-  int updays;
+  long int updays;
   int uphours;
   int upmins;
   struct tm *tmn;
@@ -73,12 +74,11 @@ print_uptime (int n, const STRUCT_UTMP *
       char *b = fgets (buf, BUFSIZ, fp);
       if (b == buf)
        {
-         /* The following sscanf must use the C locale.  */
-         setlocale (LC_NUMERIC, "C");
-         res = sscanf (buf, "%lf", &upsecs);
-         setlocale (LC_NUMERIC, "");
-         if (res == 1)
-           uptime = (time_t) upsecs;
+         char *end_ptr;
+         upsecs = c_strtod (buf, &end_ptr);
+         if (buf != end_ptr)
+           uptime = (0 <= upsecs && upsecs < TYPE_MAXIMUM (time_t)
+                     ? upsecs : -1);
        }
 
       fclose (fp);
@@ -141,9 +141,14 @@ print_uptime (int n, const STRUCT_UTMP *
            tmn->tm_min, (tmn->tm_hour < 12 ? _("am") : _("pm")));
   else
     printf (_(" ??:????  up "));
-  if (updays > 0)
-    printf (ngettext("%d day", "%d days", updays), updays);
-  printf (" %2d:%02d,  ", uphours, upmins);
+  if (uptime == (time_t) -1)
+    printf (_("???? days ??:??,  "));
+  else
+    {
+      if (0 < updays)
+       printf (ngettext ("%ld day", "%ld days", updays), updays);
+      printf (" %2d:%02d,  ", uphours, upmins);
+    }
   printf (ngettext ("%d user", "%d users", entries), entries);
 
 #if defined (HAVE_GETLOADAVG) || defined (C_GETLOADAVG)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]