diff src/editfns.c @ 90375:e6bf73e43cf4

Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-49 Merge from emacs--devo--0 Patches applied: * emacs--devo--0 (patch 164-184) - Update from CVS - Merge from gnus--rel--5.10 - Update from CVS: man/mh-e.texi (Folders): Various edits. - Update from erc--emacs--0 * gnus--rel--5.10 (patch 62-70) - Merge from emacs--devo--0 - Update from CVS
author Miles Bader <miles@gnu.org>
date Tue, 28 Mar 2006 23:08:20 +0000
parents c5406394f567 23a81b585dd1
children 65ca8fb66a0d
line wrap: on
line diff
--- a/src/editfns.c	Tue Mar 28 11:24:02 2006 +0000
+++ b/src/editfns.c	Tue Mar 28 23:08:20 2006 +0000
@@ -72,6 +72,8 @@
 extern char **environ;
 #endif
 
+#define TM_YEAR_BASE 1900
+
 extern size_t emacs_strftimeu P_ ((char *, size_t, const char *,
 				   const struct tm *, int));
 static int tm_diff P_ ((struct tm *, struct tm *));
@@ -719,7 +721,7 @@
   int orig_point = 0;
   int fwd;
   Lisp_Object prev_old, prev_new;
-  
+
   if (NILP (new_pos))
     /* Use the current point, and afterwards, set it.  */
     {
@@ -734,7 +736,7 @@
 
   prev_old = make_number (XFASTINT (old_pos) - 1);
   prev_new = make_number (XFASTINT (new_pos) - 1);
-  
+
   if (NILP (Vinhibit_field_text_motion)
       && !EQ (new_pos, old_pos)
       && (!NILP (Fget_char_property (new_pos, Qfield, Qnil))
@@ -1720,7 +1722,9 @@
   XSETFASTINT (list_args[2], decoded_time->tm_hour);
   XSETFASTINT (list_args[3], decoded_time->tm_mday);
   XSETFASTINT (list_args[4], decoded_time->tm_mon + 1);
-  XSETINT (list_args[5], decoded_time->tm_year + 1900);
+  /* On 64-bit machines an int is narrower than EMACS_INT, thus the
+     cast below avoids overflow in int arithmetics.  */
+  XSETINT (list_args[5], TM_YEAR_BASE + (EMACS_INT) decoded_time->tm_year);
   XSETFASTINT (list_args[6], decoded_time->tm_wday);
   list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
 
@@ -1776,7 +1780,7 @@
   tm.tm_hour = XINT (args[2]);
   tm.tm_mday = XINT (args[3]);
   tm.tm_mon = XINT (args[4]) - 1;
-  tm.tm_year = XINT (args[5]) - 1900;
+  tm.tm_year = XINT (args[5]) - TM_YEAR_BASE;
   tm.tm_isdst = -1;
 
   if (CONSP (zone))
@@ -1842,11 +1846,25 @@
 {
   time_t value;
   char buf[30];
+  struct tm *tm;
   register char *tem;
 
   if (! lisp_time_argument (specified_time, &value, NULL))
-    value = -1;
-  tem = (char *) ctime (&value);
+    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.  */
+  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))
+    error ("Specified time is not representable");
+  tem = asctime (tm);
 
   strncpy (buf, tem, 24);
   buf[24] = 0;
@@ -1854,8 +1872,6 @@
   return build_string (buf);
 }
 
-#define TM_YEAR_BASE 1900
-
 /* Yield A - B, measured in seconds.
    This function is copied from the GNU C Library.  */
 static int