From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.bugs Subject: Re: Emacs current-time-string core dump on 64-bit hosts Date: Fri, 24 Mar 2006 12:45:02 -0800 Message-ID: <87fyl71sj5.fsf@penguin.cs.ucla.edu> References: <87k6arnqsx.fsf@penguin.cs.ucla.edu> <87fyle6luu.fsf@penguin.cs.ucla.edu> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1143271664 15585 80.91.229.2 (25 Mar 2006 07:27:44 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 25 Mar 2006 07:27:44 +0000 (UTC) Cc: bug-gnu-emacs@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Mar 25 08:27:41 2006 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1FN3BR-0006Pa-O9 for geb-bug-gnu-emacs@m.gmane.org; Sat, 25 Mar 2006 08:27:38 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FN3BP-0003pi-Kd for geb-bug-gnu-emacs@m.gmane.org; Sat, 25 Mar 2006 02:27:35 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1FMtA3-0001zD-J6 for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2006 15:45:31 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1FMt9z-0001yU-F0 for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2006 15:45:31 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FMt9z-0001yR-BQ for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2006 15:45:27 -0500 Original-Received: from [131.179.128.19] (helo=kiwi.cs.ucla.edu) by monty-python.gnu.org with esmtp (Exim 4.52) id 1FMtAW-0001ig-PG; Fri, 24 Mar 2006 15:46:01 -0500 Original-Received: from penguin.cs.ucla.edu (Penguin.CS.UCLA.EDU [131.179.64.200]) by kiwi.cs.ucla.edu (8.11.7p1+Sun/8.11.7/UCLACS-5.2) with ESMTP id k2OKj2813507; Fri, 24 Mar 2006 12:45:02 -0800 (PST) Original-Received: from eggert by penguin.cs.ucla.edu with local (Exim 4.50) id 1FMt9a-0004iv-Dz; Fri, 24 Mar 2006 12:45:02 -0800 Original-To: rms@gnu.org In-Reply-To: (Richard Stallman's message of "Mon, 20 Mar 2006 20:00:50 -0500") User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) X-Mailman-Approved-At: Sat, 25 Mar 2006 02:27:33 -0500 X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:14985 Archived-At: Richard Stallman 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 * 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 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 ----