]> code.delx.au - gnu-emacs/commitdiff
* lib-src/b2m.c (main): Don't include <limits.h>.
authorPaul Eggert <eggert@twinsun.com>
Tue, 4 Apr 2006 04:13:02 +0000 (04:13 +0000)
committerPaul Eggert <eggert@twinsun.com>
Tue, 4 Apr 2006 04:13:02 +0000 (04:13 +0000)
(TM_YEAR_BASE): New macro.
(TM_YEAR_IN_ASCTIME_RANGE): Don't define if already defined, so
that s/ files can override this.  Use the more-conservative range
1000-9999.
(main): Check for asctime returning NULL.
* lib-src/fakemail.c: Likewise.
* src/editfns.c (TM_YEAR_IN_ASCTIME_RANGE): New macro, identical to
../lib-src/b2m.c and ../lib-src/editfns.c.
(Fcurrent_time_string): Use it.
Document that the year might not consume 4 columns if it's outside
the range 1000-9999.
Check for asctime failure.
Don't assume that the output string length is always exactly 24.

lib-src/ChangeLog
lib-src/b2m.c
lib-src/fakemail.c
src/ChangeLog
src/editfns.c

index 72ff56c2dfd17df311c7013914c91eb6cbbd8a58..a38c0cb6770e3dca068ed2952850352f9252178e 100644 (file)
@@ -1,3 +1,13 @@
+2006-04-02  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * b2m.c (main): Don't include <limits.h>.
+       (TM_YEAR_BASE): New macro.
+       (TM_YEAR_IN_ASCTIME_RANGE): Don't define if already defined, so
+       that s/ files can override this.  Use the more-conservative range
+       1000-9999.
+       (main): Check for asctime returning NULL.
+       * fakemail.c: Likewise.
+
 2006-03-27  Paul Eggert  <eggert@cs.ucla.edu>
 
        * b2m.c: Include <limits.h>.
index adaa736bcd9388c441b56e676ac22cb06d9cf005..a3ab9a5bb9ad4970885711846a2c248b2a90626f 100644 (file)
@@ -26,7 +26,6 @@
 #undef static
 #endif
 
-#include <limits.h>
 #include <stdio.h>
 #include <time.h>
 #include <sys/types.h>
 
 typedef int logical;
 
-/* True if TM_YEAR is a struct tm's tm_year value that is acceptable
-   to asctime.  Glibc asctime returns a useful string unless TM_YEAR
-   is nearly INT_MAX, but the C Standard lets C libraries overrun a
-   buffer if TM_YEAR needs more than 4 bytes.  */
-#ifdef __GLIBC__
-# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
-#else
+#define TM_YEAR_BASE 1900
+
+/* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
+   asctime to have well-defined behavior.  */
+#ifndef TM_YEAR_IN_ASCTIME_RANGE
 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
-    (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900)
+    (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
 #endif
 
 /*
@@ -148,9 +145,9 @@ main (argc, argv)
      Don't use 'ctime', as that might dump core if the hardware clock
      is set to a bizarre value.  */
   tm = localtime (&ltoday);
-  if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)))
+  if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)
+        && (today = asctime (tm))))
     fatal ("current time is out of range");
-  today = asctime (tm);
   data.size = 200;
   data.buffer = xnew (200, char);
 
index 2c2b462e3664a30157a6b922a93e10e93a1e5eb4..6b8634f34ab9a95f917cb82828421f54ef822f54 100644 (file)
@@ -53,7 +53,6 @@ main ()
 #include "ntlib.h"
 #endif
 
-#include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
@@ -71,15 +70,13 @@ main ()
 #define true 1
 #define false 0
 
-/* True if TM_YEAR is a struct tm's tm_year value that is acceptable
-   to asctime.  Glibc asctime returns a useful string unless TM_YEAR
-   is nearly INT_MAX, but the C Standard lets C libraries overrun a
-   buffer if TM_YEAR needs more than 4 bytes.  */
-#ifdef __GLIBC__
-# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
-#else
+#define TM_YEAR_BASE 1900
+
+/* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
+   asctime to have well-defined behavior.  */
+#ifndef TM_YEAR_IN_ASCTIME_RANGE
 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
-    (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900)
+    (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
 #endif
 
 /* Various lists */
@@ -378,9 +375,9 @@ make_file_preface ()
      Don't use 'ctime', as that might dump core if the hardware clock
      is set to a bizarre value.  */
   tm = localtime (&idiotic_interface);
-  if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)))
+  if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)
+        && (the_date = asctime (tm))))
     fatal ("current time is out of range", 0);
-  the_date = asctime (tm);
   /* the_date has an unwanted newline at the end */
   date_length = strlen (the_date) - 1;
   the_date[date_length] = '\0';
index 88a8b7b4df5449edfdcce0e3624eb2d5dacff3ea..669c5a0c0b94862c3d70c4aa94dd7c6c5b942329 100644 (file)
@@ -1,3 +1,13 @@
+2006-04-03  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * editfns.c (TM_YEAR_IN_ASCTIME_RANGE): New macro, identical to
+       ../lib-src/b2m.c and ../lib-src/editfns.c.
+       (Fcurrent_time_string): Use it.
+       Document that the year might not consume 4 columns if it's outside
+       the range 1000-9999.
+       Check for asctime failure.
+       Don't assume that the output string length is always exactly 24.
+
 2006-04-03  YAMAMOTO Mitsuharu  <mituharu@math.s.chiba-u.ac.jp>
 
        * macterm.c (XTread_socket): Initialize variable `f' before its use.
index 888bbe3062b2adcbe212c6b1d603dc012774a52e..450a7684584b59953ea38da8d3baae462565f546 100644 (file)
@@ -74,6 +74,13 @@ extern char **environ;
 
 #define TM_YEAR_BASE 1900
 
+/* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
+   asctime to have well-defined behavior.  */
+#ifndef TM_YEAR_IN_ASCTIME_RANGE
+# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
+    (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
+#endif
+
 extern size_t emacs_strftimeu P_ ((char *, size_t, const char *,
                                   const struct tm *, int));
 static int tm_diff P_ ((struct tm *, struct tm *));
@@ -1833,7 +1840,8 @@ usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)  */)
 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
        doc: /* Return the current time, as a human-readable string.
 Programs can use this function to decode a time,
-since the number of columns in each field is fixed.
+since the number of columns in each field is fixed
+if the year is in the range 1000-9999.
 The format is `Sun Sep 16 01:03:52 1973'.
 However, see also the functions `decode-time' and `format-time-string'
 which provide a much more powerful and general facility.
@@ -1847,31 +1855,23 @@ but this is considered obsolete.  */)
      Lisp_Object specified_time;
 {
   time_t value;
-  char buf[30];
   struct tm *tm;
   register char *tem;
 
   if (! lisp_time_argument (specified_time, &value, NULL))
     error ("Invalid time specification");
-  /* Do not use ctime, since it has undefined behavior with
-     out-of-range time stamps.  This avoids a core dump triggered by
-     (current-time-string '(2814749767106 0)) on 64-bit Solaris 8. See
-     <http://www.opengroup.org/austin/mailarchives/ag/msg09294.html>
-     for more details about this portability problem.  */
+
+  /* Convert to a string, checking for out-of-range time stamps.
+     Don't use 'ctime', as that might dump core if VALUE is out of
+     range.  */
   tm = localtime (&value);
-  /* Checking for out-of-range time stamps avoids buffer overruns that
-     cause core dump on some systems (e.g., 64-bit Solaris), and also
-     preserves the historic behavior of always returning a fixed-size
-     24-character string.  */
-  if (! (tm && -999 - TM_YEAR_BASE <= tm->tm_year
-        && tm->tm_year <= 9999 - TM_YEAR_BASE))
+  if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year) && (tem = asctime (tm))))
     error ("Specified time is not representable");
-  tem = asctime (tm);
 
-  strncpy (buf, tem, 24);
-  buf[24] = 0;
+  /* Remove the trailing newline.  */
+  tem[strlen (tem) - 1] = '\0';
 
-  return build_string (buf);
+  return build_string (tem);
 }
 
 /* Yield A - B, measured in seconds.