]> granicus.if.org Git - python/commitdiff
check possible recursive _as_parameter_ to prevent segfault (closes #1838)
authorBenjamin Peterson <benjamin@python.org>
Sat, 26 Mar 2011 22:56:28 +0000 (17:56 -0500)
committerBenjamin Peterson <benjamin@python.org>
Sat, 26 Mar 2011 22:56:28 +0000 (17:56 -0500)
Lib/ctypes/test/test_as_parameter.py
Lib/lib2to3/refactor.py
Lib/lib2to3/tests/test_refactor.py
Misc/NEWS
Modules/_ctypes/_ctypes.c

index f2539b12a5d2371c36a1cd25f352d6854bf6b413..7e8017345a97634f26a7c1359377f467e813fc5f 100644 (file)
@@ -187,6 +187,18 @@ class BasicWrapTestCase(unittest.TestCase):
         self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
                              (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
 
+    def test_recursive_as_param(self):
+        from ctypes import c_int
+
+        class A(object):
+            pass
+
+        a = A()
+        a._as_parameter_ = a
+        with self.assertRaises(RuntimeError):
+            c_int.from_param(a)
+
+
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 class AsParamWrapper(object):
index 7d00d12f9d8054654da9214e7aba5a7a4f0b696c..30aea631b95b3bb67a4e6c9ffcf3ee74e271adb7 100644 (file)
@@ -500,7 +500,7 @@ class RefactoringTool(object):
                         node = new
 
     def processed_file(self, new_text, filename, old_text=None, write=False,
-                       encoding=None):
+                       encoding=None, newlines=None):
         """
         Called when a file has been refactored, and there are changes.
         """
index 3eecde8cfb667a3963f6973d2c0bac63a6bf0ec9..ed759551394615e4015f4400780352821b16876d 100644 (file)
@@ -231,6 +231,23 @@ from __future__ import print_function"""
                 os.path.join("a_dir", "stuff.py")]
         check(tree, tree)
 
+    def test_preserve_file_newlines(self):
+        rt = self.rt(fixers=_2TO3_FIXERS)
+        for nl in ("\r\n", "\n"):
+            data = "print y%s%syes%sok%s" % ((nl,) * 4)
+            handle, tmp = tempfile.mkstemp()
+            os.close(handle)
+            try:
+                with open(tmp, "w") as fp:
+                    fp.write(data)
+                rt.refactor_file(tmp)
+                with open(tmp, "r") as fp:
+                    contents = fp.read()
+            finally:
+                os.unlink(tmp)
+            for line in contents.splitlines(True):
+                self.assertTrue(line.endswith(nl))
+
     def test_file_encoding(self):
         fn = os.path.join(TEST_DATA_DIR, "different_encoding.py")
         self.check_file_refactoring(fn)
index 151bc1c48732e47153041f5e1c822853d6ec9d79..3c590aceea1de6c35b6d1d635ddabaa706ba6714 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -245,6 +245,9 @@ Library
 Extension Modules
 -----------------
 
+- Issue #1838: Prevent segfault in ctypes, when _as_parameter_ on a class is set
+  to an instance of the class.
+
 - Issue #678250: Make mmap flush a noop on ACCESS_READ and ACCESS_COPY.
 
 Build
index bb406c6218a32976f196fc0f76c0f632776bb194..d00857621e4a685fc62b205344788e077ecd99b7 100644 (file)
@@ -2065,10 +2065,14 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
     PyCArgObject *parg;
     struct fielddesc *fd;
     PyObject *as_parameter;
+    int res;
 
     /* If the value is already an instance of the requested type,
        we can use it as is */
-    if (1 == PyObject_IsInstance(value, type)) {
+    res = PyObject_IsInstance(value, type);
+    if (res == -1)
+        return NULL;
+    if (res) {
         Py_INCREF(value);
         return value;
     }
@@ -2097,7 +2101,12 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
 
     as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
     if (as_parameter) {
+        if (Py_EnterRecursiveCall("while processing _as_parameter_")) {
+            Py_DECREF(as_parameter);
+            return NULL;
+        }
         value = PyCSimpleType_from_param(type, as_parameter);
+        Py_LeaveRecursiveCall();
         Py_DECREF(as_parameter);
         return value;
     }