aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2006-04-04 04:13:02 +0000
committerPaul Eggert2006-04-04 04:13:02 +0000
commitd65b42351fbebde2539d447ecf5443e1725bdce3 (patch)
treedeb0a158f63a41290877cd94550db6d95bd4d500
parentd4d0c924b1b4b6d0a20b21b791b0226ba579f5fb (diff)
downloademacs-d65b42351fbebde2539d447ecf5443e1725bdce3.tar.gz
emacs-d65b42351fbebde2539d447ecf5443e1725bdce3.zip
* lib-src/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. * 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.
-rw-r--r--lib-src/ChangeLog10
-rw-r--r--lib-src/b2m.c19
-rw-r--r--lib-src/fakemail.c19
-rw-r--r--src/ChangeLog10
-rw-r--r--src/editfns.c34
5 files changed, 53 insertions, 39 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog
index 72ff56c2dfd..a38c0cb6770 100644
--- a/lib-src/ChangeLog
+++ b/lib-src/ChangeLog
@@ -1,3 +1,13 @@
12006-04-02 Paul Eggert <eggert@cs.ucla.edu>
2
3 * b2m.c (main): Don't include <limits.h>.
4 (TM_YEAR_BASE): New macro.
5 (TM_YEAR_IN_ASCTIME_RANGE): Don't define if already defined, so
6 that s/ files can override this. Use the more-conservative range
7 1000-9999.
8 (main): Check for asctime returning NULL.
9 * fakemail.c: Likewise.
10
12006-03-27 Paul Eggert <eggert@cs.ucla.edu> 112006-03-27 Paul Eggert <eggert@cs.ucla.edu>
2 12
3 * b2m.c: Include <limits.h>. 13 * b2m.c: Include <limits.h>.
diff --git a/lib-src/b2m.c b/lib-src/b2m.c
index adaa736bcd9..a3ab9a5bb9a 100644
--- a/lib-src/b2m.c
+++ b/lib-src/b2m.c
@@ -26,7 +26,6 @@
26#undef static 26#undef static
27#endif 27#endif
28 28
29#include <limits.h>
30#include <stdio.h> 29#include <stdio.h>
31#include <time.h> 30#include <time.h>
32#include <sys/types.h> 31#include <sys/types.h>
@@ -45,15 +44,13 @@
45 44
46typedef int logical; 45typedef int logical;
47 46
48/* True if TM_YEAR is a struct tm's tm_year value that is acceptable 47#define TM_YEAR_BASE 1900
49 to asctime. Glibc asctime returns a useful string unless TM_YEAR 48
50 is nearly INT_MAX, but the C Standard lets C libraries overrun a 49/* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
51 buffer if TM_YEAR needs more than 4 bytes. */ 50 asctime to have well-defined behavior. */
52#ifdef __GLIBC__ 51#ifndef TM_YEAR_IN_ASCTIME_RANGE
53# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
54#else
55# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \ 52# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
56 (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900) 53 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
57#endif 54#endif
58 55
59/* 56/*
@@ -148,9 +145,9 @@ main (argc, argv)
148 Don't use 'ctime', as that might dump core if the hardware clock 145 Don't use 'ctime', as that might dump core if the hardware clock
149 is set to a bizarre value. */ 146 is set to a bizarre value. */
150 tm = localtime (&ltoday); 147 tm = localtime (&ltoday);
151 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year))) 148 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)
149 && (today = asctime (tm))))
152 fatal ("current time is out of range"); 150 fatal ("current time is out of range");
153 today = asctime (tm);
154 data.size = 200; 151 data.size = 200;
155 data.buffer = xnew (200, char); 152 data.buffer = xnew (200, char);
156 153
diff --git a/lib-src/fakemail.c b/lib-src/fakemail.c
index 2c2b462e366..6b8634f34ab 100644
--- a/lib-src/fakemail.c
+++ b/lib-src/fakemail.c
@@ -53,7 +53,6 @@ main ()
53#include "ntlib.h" 53#include "ntlib.h"
54#endif 54#endif
55 55
56#include <limits.h>
57#include <stdio.h> 56#include <stdio.h>
58#include <string.h> 57#include <string.h>
59#include <ctype.h> 58#include <ctype.h>
@@ -71,15 +70,13 @@ main ()
71#define true 1 70#define true 1
72#define false 0 71#define false 0
73 72
74/* True if TM_YEAR is a struct tm's tm_year value that is acceptable 73#define TM_YEAR_BASE 1900
75 to asctime. Glibc asctime returns a useful string unless TM_YEAR 74
76 is nearly INT_MAX, but the C Standard lets C libraries overrun a 75/* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
77 buffer if TM_YEAR needs more than 4 bytes. */ 76 asctime to have well-defined behavior. */
78#ifdef __GLIBC__ 77#ifndef TM_YEAR_IN_ASCTIME_RANGE
79# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
80#else
81# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \ 78# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
82 (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900) 79 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
83#endif 80#endif
84 81
85/* Various lists */ 82/* Various lists */
@@ -378,9 +375,9 @@ make_file_preface ()
378 Don't use 'ctime', as that might dump core if the hardware clock 375 Don't use 'ctime', as that might dump core if the hardware clock
379 is set to a bizarre value. */ 376 is set to a bizarre value. */
380 tm = localtime (&idiotic_interface); 377 tm = localtime (&idiotic_interface);
381 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year))) 378 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)
379 && (the_date = asctime (tm))))
382 fatal ("current time is out of range", 0); 380 fatal ("current time is out of range", 0);
383 the_date = asctime (tm);
384 /* the_date has an unwanted newline at the end */ 381 /* the_date has an unwanted newline at the end */
385 date_length = strlen (the_date) - 1; 382 date_length = strlen (the_date) - 1;
386 the_date[date_length] = '\0'; 383 the_date[date_length] = '\0';
diff --git a/src/ChangeLog b/src/ChangeLog
index 88a8b7b4df5..669c5a0c0b9 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,13 @@
12006-04-03 Paul Eggert <eggert@cs.ucla.edu>
2
3 * editfns.c (TM_YEAR_IN_ASCTIME_RANGE): New macro, identical to
4 ../lib-src/b2m.c and ../lib-src/editfns.c.
5 (Fcurrent_time_string): Use it.
6 Document that the year might not consume 4 columns if it's outside
7 the range 1000-9999.
8 Check for asctime failure.
9 Don't assume that the output string length is always exactly 24.
10
12006-04-03 YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp> 112006-04-03 YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp>
2 12
3 * macterm.c (XTread_socket): Initialize variable `f' before its use. 13 * macterm.c (XTread_socket): Initialize variable `f' before its use.
diff --git a/src/editfns.c b/src/editfns.c
index 888bbe3062b..450a7684584 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -74,6 +74,13 @@ extern char **environ;
74 74
75#define TM_YEAR_BASE 1900 75#define TM_YEAR_BASE 1900
76 76
77/* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
78 asctime to have well-defined behavior. */
79#ifndef TM_YEAR_IN_ASCTIME_RANGE
80# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
81 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
82#endif
83
77extern size_t emacs_strftimeu P_ ((char *, size_t, const char *, 84extern size_t emacs_strftimeu P_ ((char *, size_t, const char *,
78 const struct tm *, int)); 85 const struct tm *, int));
79static int tm_diff P_ ((struct tm *, struct tm *)); 86static int tm_diff P_ ((struct tm *, struct tm *));
@@ -1833,7 +1840,8 @@ usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */)
1833DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0, 1840DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
1834 doc: /* Return the current time, as a human-readable string. 1841 doc: /* Return the current time, as a human-readable string.
1835Programs can use this function to decode a time, 1842Programs can use this function to decode a time,
1836since the number of columns in each field is fixed. 1843since the number of columns in each field is fixed
1844if the year is in the range 1000-9999.
1837The format is `Sun Sep 16 01:03:52 1973'. 1845The format is `Sun Sep 16 01:03:52 1973'.
1838However, see also the functions `decode-time' and `format-time-string' 1846However, see also the functions `decode-time' and `format-time-string'
1839which provide a much more powerful and general facility. 1847which provide a much more powerful and general facility.
@@ -1847,31 +1855,23 @@ but this is considered obsolete. */)
1847 Lisp_Object specified_time; 1855 Lisp_Object specified_time;
1848{ 1856{
1849 time_t value; 1857 time_t value;
1850 char buf[30];
1851 struct tm *tm; 1858 struct tm *tm;
1852 register char *tem; 1859 register char *tem;
1853 1860
1854 if (! lisp_time_argument (specified_time, &value, NULL)) 1861 if (! lisp_time_argument (specified_time, &value, NULL))
1855 error ("Invalid time specification"); 1862 error ("Invalid time specification");
1856 /* Do not use ctime, since it has undefined behavior with 1863
1857 out-of-range time stamps. This avoids a core dump triggered by 1864 /* Convert to a string, checking for out-of-range time stamps.
1858 (current-time-string '(2814749767106 0)) on 64-bit Solaris 8. See 1865 Don't use 'ctime', as that might dump core if VALUE is out of
1859 <http://www.opengroup.org/austin/mailarchives/ag/msg09294.html> 1866 range. */
1860 for more details about this portability problem. */
1861 tm = localtime (&value); 1867 tm = localtime (&value);
1862 /* Checking for out-of-range time stamps avoids buffer overruns that 1868 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year) && (tem = asctime (tm))))
1863 cause core dump on some systems (e.g., 64-bit Solaris), and also
1864 preserves the historic behavior of always returning a fixed-size
1865 24-character string. */
1866 if (! (tm && -999 - TM_YEAR_BASE <= tm->tm_year
1867 && tm->tm_year <= 9999 - TM_YEAR_BASE))
1868 error ("Specified time is not representable"); 1869 error ("Specified time is not representable");
1869 tem = asctime (tm);
1870 1870
1871 strncpy (buf, tem, 24); 1871 /* Remove the trailing newline. */
1872 buf[24] = 0; 1872 tem[strlen (tem) - 1] = '\0';
1873 1873
1874 return build_string (buf); 1874 return build_string (tem);
1875} 1875}
1876 1876
1877/* Yield A - B, measured in seconds. 1877/* Yield A - B, measured in seconds.