Serhiy Storchaka added the comment:

Thank you for comments, Victor. Here is an updated patch.

----------
Added file: http://bugs.python.org/file28963/asctime_s_2.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue16137>
_______________________________________
diff -r 20f0c5398e97 Lib/test/test_time.py
--- a/Lib/test/test_time.py     Mon Feb 04 10:29:38 2013 -0500
+++ b/Lib/test/test_time.py     Tue Feb 05 16:04:32 2013 +0200
@@ -128,10 +128,15 @@
         # self.assertRaises(ValueError, time.asctime,
         #                  (12345, 1, 0, 0, 0, 0, 0, 0, 0))
         # XXX: For now, just make sure we don't have a crash:
-        try:
-            time.asctime((12345, 1, 1, 0, 0, 0, 0, 1, 0))
-        except ValueError:
-            pass
+        def check(tm):
+            try:
+                self.assertNotIn('\n', time.asctime(tm))
+            except ValueError:
+                pass
+        check((12345, 1, 1, 0, 0, 0, 0, 1, 0))
+        # Issue #16137
+        check((2013, 2, 5, -1, 0, 0, 0, 0, 0))
+        check((2013, 2, 5, 101, 0, 0, 0, 0, 0))
 
     @unittest.skipIf(not hasattr(time, "tzset"),
         "time module has no attribute tzset")
diff -r 20f0c5398e97 Misc/NEWS
--- a/Misc/NEWS Mon Feb 04 10:29:38 2013 -0500
+++ b/Misc/NEWS Tue Feb 05 16:04:32 2013 +0200
@@ -199,6 +199,10 @@
 Library
 -------
 
+- Issue #16137: Fix a segmentation fault when time.asctime() called with an
+  illegal time (i.e. with a negative hour) on Windows.  On other platforms a
+  result of such call no more contains a trailing '\n'.
+
 - Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
   parses nested mutating sequence.
 
diff -r 20f0c5398e97 Modules/timemodule.c
--- a/Modules/timemodule.c      Mon Feb 04 10:29:38 2013 -0500
+++ b/Modules/timemodule.c      Tue Feb 05 16:04:32 2013 +0200
@@ -563,7 +563,10 @@
 {
     PyObject *tup = NULL;
     struct tm buf;
-    char *p;
+    char *p, *q;
+#ifdef MS_WINDOWS
+    char sbuf[100];
+#endif
     if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
         return NULL;
     if (tup == NULL) {
@@ -571,14 +574,24 @@
         buf = *localtime(&tt);
     } else if (!gettmarg(tup, &buf))
         return NULL;
+#ifdef MS_WINDOWS
+    if (asctime_s(sbuf, sizeof(sbuf) - 1, &buf) != 0)
+        p = NULL;
+    else {
+        p = sbuf;
+        sbuf[sizeof(sbuf) - 1] = '\0';
+    }
+#else
     p = asctime(&buf);
+#endif
     if (p == NULL) {
         PyErr_SetString(PyExc_ValueError, "invalid time");
         return NULL;
     }
-    if (p[24] == '\n')
-        p[24] = '\0';
-    return PyString_FromString(p);
+    q = strchr(p, '\n');
+    if (q == NULL)
+        q = strchr(p, '\0'); /* end of string */
+    return PyString_FromStringAndSize(p, q - p);
 }
 
 PyDoc_STRVAR(asctime_doc,
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to