[Python-checkins] cpython (3.3): - Issue #17782: Fix undefined behaviour on platforms where ``struct

antoine.pitrou python-checkins at python.org
Wed Apr 17 22:10:45 CEST 2013


http://hg.python.org/cpython/rev/7a0fb0f59cf6
changeset:   83425:7a0fb0f59cf6
branch:      3.3
parent:      83422:65623d7dc76e
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Wed Apr 17 22:06:44 2013 +0200
summary:
  - Issue #17782: Fix undefined behaviour on platforms where ``struct timespec``'s "tv_nsec" member is not a C long.

files:
  Misc/NEWS              |   3 +++
  Modules/posixmodule.c  |  10 ++++++++--
  Modules/signalmodule.c |   6 +++++-
  Modules/timemodule.c   |   6 +++++-
  4 files changed, 21 insertions(+), 4 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #17782: Fix undefined behaviour on platforms where
+  ``struct timespec``'s "tv_nsec" member is not a C long.
+
 - Issue #17715: Fix segmentation fault from raising an exception in a __trunc__
   method.
 
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -4775,6 +4775,8 @@
     }
 
     if (times && (times != Py_None)) {
+        time_t a_sec, m_sec;
+        long a_nsec, m_nsec;
         if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
             PyErr_SetString(PyExc_TypeError,
                          "utime: 'times' must be either"
@@ -4783,11 +4785,15 @@
         }
         utime.now = 0;
         if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
-                                     &utime.atime_s, &utime.atime_ns) == -1 ||
+                                     &a_sec, &a_nsec) == -1 ||
             _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
-                                     &utime.mtime_s, &utime.mtime_ns) == -1) {
+                                     &m_sec, &m_nsec) == -1) {
             goto exit;
         }
+        utime.atime_s = a_sec;
+        utime.atime_ns = a_nsec;
+        utime.mtime_s = m_sec;
+        utime.mtime_ns = m_nsec;
     }
     else if (ns) {
         if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -784,14 +784,18 @@
     struct timespec buf;
     sigset_t set;
     siginfo_t si;
+    time_t tv_sec;
+    long tv_nsec;
     int err;
 
     if (!PyArg_ParseTuple(args, "OO:sigtimedwait",
                           &signals, &timeout))
         return NULL;
 
-    if (_PyTime_ObjectToTimespec(timeout, &buf.tv_sec, &buf.tv_nsec) == -1)
+    if (_PyTime_ObjectToTimespec(timeout, &tv_sec, &tv_nsec) == -1)
         return NULL;
+    buf.tv_sec = tv_sec;
+    buf.tv_nsec = tv_nsec;
 
     if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
         PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -195,14 +195,18 @@
 {
     int clk_id;
     PyObject *obj;
+    time_t tv_sec;
+    long tv_nsec;
     struct timespec tp;
     int ret;
 
     if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
         return NULL;
 
-    if (_PyTime_ObjectToTimespec(obj, &tp.tv_sec, &tp.tv_nsec) == -1)
+    if (_PyTime_ObjectToTimespec(obj, &tv_sec, &tv_nsec) == -1)
         return NULL;
+    tp.tv_sec = tv_sec;
+    tp.tv_nsec = tv_nsec;
 
     ret = clock_settime((clockid_t)clk_id, &tp);
     if (ret != 0) {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list