unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@CS.UCLA.EDU>
Cc: bug-gnu-emacs@gnu.org
Subject: Re: Emacs current-time-string core dump on 64-bit hosts
Date: Fri, 24 Mar 2006 12:45:02 -0800	[thread overview]
Message-ID: <87fyl71sj5.fsf@penguin.cs.ucla.edu> (raw)
In-Reply-To: <E1FLVEw-0001t4-PZ@fencepost.gnu.org> (Richard Stallman's message of "Mon, 20 Mar 2006 20:00:50 -0500")

Richard Stallman <rms@gnu.org> writes:

> Would you please put the new code directly into editfns.c?
> That would avoid the need for new files, makefile changes, etc.

OK, I've done this, and installed the following patch.  This patch is
simpler than what I submitted earlier, since it uses asctime rather
than formatting by hand.

2006-03-24  Paul Eggert  <eggert@cs.ucla.edu>

	* editfns.c: Do not use ctime, since it has undefined behavior
	with out-of-range time stamps.  This fixes a bug where
	(current-time-string '(2814749767106 0)) would make Emacs dump
	core on 64-bit Solaris 8.  The fix is to use localtime+asctime
	(checking for in-range results) instead of ctime.  Please see
	<http://www.opengroup.org/austin/mailarchives/ag/msg09294.html>
	for more details about this portability problem.
	(TM_YEAR_BASE): Move up, so the changes below can use it.
	(Fdecode_time, Fencode_time): Use TM_YEAR_BASE instead of 1900.
	(Fdecode_time): Cast tm_year to EMACS_INT, to avoid overflow when
	int is narrower than EMACS_INT.
	(Fcurrent_time_string): As with Fformat_time_string, report an
	invalid time specification if the argument is invalid.  Also,
	check for out-of-range time stamps; this prevents a buffer overrun
	that causes Emacs to dump core on 64-bit Solaris sparc, and it
	preserves the historic behavior of always returning a fixed-size
	string.

*** editfns.c	7 Feb 2006 09:08:53 -0000	1.409
--- editfns.c	24 Mar 2006 20:40:24 -0000	1.410
*************** Boston, MA 02110-1301, USA.  */
*** 72,77 ****
--- 72,79 ----
  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 *));
*************** DOW and ZONE.)  */)
*** 1722,1728 ****
    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);
    XSETFASTINT (list_args[6], decoded_time->tm_wday);
    list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil;
  
--- 1724,1730 ----
    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], 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;
  
*************** usage: (encode-time SECOND MINUTE HOUR D
*** 1778,1784 ****
    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_isdst = -1;
  
    if (CONSP (zone))
--- 1780,1786 ----
    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]) - TM_YEAR_BASE;
    tm.tm_isdst = -1;
  
    if (CONSP (zone))
*************** but this is considered obsolete.  */)
*** 1844,1854 ****
  {
    time_t value;
    char buf[30];
    register char *tem;
  
    if (! lisp_time_argument (specified_time, &value, NULL))
!     value = -1;
!   tem = (char *) ctime (&value);
  
    strncpy (buf, tem, 24);
    buf[24] = 0;
--- 1846,1861 ----
  {
    time_t value;
    char buf[30];
+   struct tm *tm;
    register char *tem;
  
    if (! lisp_time_argument (specified_time, &value, NULL))
!     error ("Invalid time specification");
!   tm = localtime (&value);
!   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;
*************** but this is considered obsolete.  */)
*** 1856,1863 ****
    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
--- 1863,1868 ----

  reply	other threads:[~2006-03-24 20:45 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-17  5:58 Emacs current-time-string core dump on 64-bit hosts Paul Eggert
2006-03-17 12:16 ` Eli Zaretskii
2006-03-17 12:46   ` Andreas Schwab
2006-03-17 16:04   ` Kevin Rodgers
2006-03-18  0:44   ` Paul Eggert
2006-03-18 11:50     ` Eli Zaretskii
2006-03-19  2:30       ` Paul Eggert
2006-03-21 19:25         ` Richard Stallman
2006-03-18  8:43   ` Richard Stallman
2006-03-19  1:53     ` Paul Eggert
2006-03-19 21:50       ` Richard Stallman
2006-03-19 23:44         ` Paul Eggert
2006-03-20 19:59           ` Eli Zaretskii
2006-03-27 22:00             ` Paul Eggert
2006-03-28 10:16               ` Eli Zaretskii
2006-03-30  7:52                 ` Paul Eggert
2006-03-30 20:36                   ` Eli Zaretskii
2006-04-04  4:57                   ` Paul Eggert
2006-04-04 18:20                     ` Eli Zaretskii
2006-03-21  1:00           ` Richard Stallman
2006-03-24 20:45             ` Paul Eggert [this message]
2006-03-25  9:10               ` Eli Zaretskii
2006-03-26  5:25                 ` Paul Eggert
2006-03-26 20:06                   ` Eli Zaretskii
2006-03-27 22:29                     ` Richard Stallman
2006-03-28 10:20                       ` Eli Zaretskii
2006-03-29  8:14                         ` Richard Stallman
2006-03-25 15:26               ` Richard Stallman
2006-03-24 21:00             ` Paul Eggert
2006-03-24 21:09             ` Paul Eggert
2006-03-25 15:26               ` Richard Stallman
2006-03-26  7:31                 ` Paul Eggert
     [not found]                   ` <E1FNnCd-0000pN-J4@fencepost.gnu.org>
2006-03-27 20:49                     ` Paul Eggert
2006-03-28 19:33                       ` Richard Stallman
2006-03-30  7:57                         ` Paul Eggert
2006-03-31 17:28                           ` Richard Stallman
2006-03-31 20:51                             ` Paul Eggert
2006-04-01 20:28                               ` Richard Stallman
2006-04-03  4:44                                 ` Paul Eggert
2006-03-17 16:25 ` Andreas Schwab
  -- strict thread matches above, loose matches on Subject: below --
2006-03-17  8:02 Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fyl71sj5.fsf@penguin.cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=bug-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).