This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Spurious newline in time.ctime
Type: enhancement Stage: needs patch
Components: Extension Modules Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder: time.asctime segfaults when given a time in the far future
View: 8013
Assigned To: belopolsky Nosy List: Gerrit.Holl, belopolsky, eric.smith, georg.brandl
Priority: normal Keywords: easy, patch

Created on 2010-11-28 15:47 by Gerrit.Holl, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (9)
msg122665 - (view) Author: Gerrit Holl (Gerrit.Holl) * Date: 2010-11-28 15:47
When the time passed to time.ctime is large, it adds a newline:

>>> import time
>>> time.ctime(2<<36)
'Wed Apr  8 17:04:32 6325'
>>> time.ctime(2<<37)
'Wed Jul 14 08:09:04 10680\n'
msg122670 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-11-28 16:09
There's an error in time_ctime where it expects the length of the string to be fixed:

    if (p[24] == '\n')
        p[24] = '\0';

It doesn't count on the year having 5 digits. It should probably say (untested):
    l = len(p);
    if (l > 0 && p[l-1] == '\n')
        p[l-1] = '\0';

I'll whip up a patch and some tests. I'm not sure if the tests will work on platforms other than Linux. I'll see what I can find out about ctime and large values.
msg122671 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-11-28 16:10
That should be strlen(), of course.
msg122673 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-11-28 16:12
What platform are you running this on? My Fedora 32 bit system won't support a time_t that large.
msg122674 - (view) Author: Gerrit Holl (Gerrit.Holl) * Date: 2010-11-28 16:14
I'm on a 64bit system (kubuntu lucid lynx)

$ uname -a
Linux sjisjka 2.6.32-25-generic #45-Ubuntu SMP Sat Oct 16 19:52:42 UTC 2010 x86_64 GNU/Linux
msg122675 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-11-28 16:17
Can you try this diff and see if it solves the problem:

Index: Modules/timemodule.c
===================================================================
--- Modules/timemodule.c        (revision 86848)
+++ Modules/timemodule.c        (working copy)
@@ -639,6 +639,7 @@
     PyObject *ot = NULL;
     time_t tt;
     char *p;
+    Py_ssize_t len;
 
     if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot))
         return NULL;
@@ -657,8 +658,10 @@
         PyErr_SetString(PyExc_ValueError, "unconvertible time");
         return NULL;
     }
-    if (p[24] == '\n')
-        p[24] = '\0';
+    len = strlen(p);
+    if (len > 0 && p[len-1] == '\n')
+        /* Remove the trailing newline, if present. */
+        p[len-1] = '\0';
     return PyUnicode_FromString(p);
 }
msg122678 - (view) Author: Gerrit Holl (Gerrit.Holl) * Date: 2010-11-28 16:36
Yes, this solves the issue:

$ ./python 
Python 3.1.3 (r313:86834, Nov 28 2010, 17:34:23) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.ctime(2<<40)
'Fri Apr 10 03:12:32 71654'
msg122761 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-11-29 03:18
This looks like a bug in the underlying platform. POSIX requires [1] that the output of ctime() fits in a 26-character buffer.  Note that a change has been recently made to time.asctime() to reject year > 9999.  See r85137 and issue6608.  I think for consistency, we should reject huge timestamps in time ctime() as well. 


[1] http://www.opengroup.org/onlinepubs/009695399/functions/ctime.html
msg125223 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-01-03 20:02
This has now been superseded by the changes made for issue #8013.
History
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54772
2011-01-03 20:02:25georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg125223

superseder: time.asctime segfaults when given a time in the far future
resolution: out of date
2010-11-29 03:21:21belopolskysetassignee: belopolsky
stage: test needed -> needs patch
type: enhancement
versions: + Python 3.2, - Python 2.6, Python 3.1, Python 2.7
2010-11-29 03:18:57belopolskysetnosy: + belopolsky
messages: + msg122761
2010-11-28 16:45:19eric.smithsetkeywords: + patch, easy
nosy: eric.smith, Gerrit.Holl
components: + Extension Modules, - Library (Lib)
stage: test needed
2010-11-28 16:36:39Gerrit.Hollsetmessages: + msg122678
2010-11-28 16:17:31eric.smithsetmessages: + msg122675
2010-11-28 16:14:04Gerrit.Hollsetmessages: + msg122674
2010-11-28 16:12:51eric.smithsetmessages: + msg122673
2010-11-28 16:10:22eric.smithsetmessages: + msg122671
2010-11-28 16:09:29eric.smithsetnosy: + eric.smith
messages: + msg122670
2010-11-28 15:47:16Gerrit.Hollcreate