]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44720: Don't crash when calling weakref.proxy(not_an_iterator).__next__ (GH-27316)
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Sat, 24 Jul 2021 09:17:19 +0000 (05:17 -0400)
committerGitHub <noreply@github.com>
Sat, 24 Jul 2021 09:17:19 +0000 (11:17 +0200)
Lib/test/test_weakref.py
Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst [new file with mode: 0644]
Objects/weakrefobject.c

index dd5a781ed59d8ba87939b50232d0b2af011c3188..1a5314ccff315a4d216658fdd80cab1eb0d57392 100644 (file)
@@ -411,6 +411,36 @@ class ReferencesTestCase(TestBase):
             # can be killed in the middle of the call
             "blech" in p
 
+    def test_proxy_next(self):
+        arr = [4, 5, 6]
+        def iterator_func():
+            yield from arr
+        it = iterator_func()
+
+        class IteratesWeakly:
+            def __iter__(self):
+                return weakref.proxy(it)
+
+        weak_it = IteratesWeakly()
+
+        # Calls proxy.__next__
+        self.assertEqual(list(weak_it), [4, 5, 6])
+
+    def test_proxy_bad_next(self):
+        # bpo-44720: PyIter_Next() shouldn't be called if the reference
+        # isn't an iterator.
+
+        not_an_iterator = lambda: 0
+
+        class A:
+            def __iter__(self):
+                return weakref.proxy(not_an_iterator)
+        a = A()
+
+        msg = "Weakref proxy referenced a non-iterator"
+        with self.assertRaisesRegex(TypeError, msg):
+            list(a)
+
     def test_proxy_reversed(self):
         class MyObj:
             def __len__(self):
diff --git a/Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst b/Misc/NEWS.d/next/Library/2021-07-24-02-17-59.bpo-44720.shU5Qm.rst
new file mode 100644 (file)
index 0000000..83694f3
--- /dev/null
@@ -0,0 +1 @@
+``weakref.proxy`` objects referencing non-iterators now raise ``TypeError`` rather than dereferencing the null ``tp_iternext`` slot and crashing.
\ No newline at end of file
index c36d2395cc8c53901635dab012e3355bfe097cd3..bb56c7dbdb8322915f1da039a9fe933124c3bc3d 100644 (file)
@@ -657,6 +657,12 @@ proxy_iternext(PyWeakReference *proxy)
         return NULL;
 
     PyObject *obj = PyWeakref_GET_OBJECT(proxy);
+    if (!PyIter_Check(obj)) {
+        PyErr_Format(PyExc_TypeError,
+            "Weakref proxy referenced a non-iterator '%.200s' object",
+            Py_TYPE(obj)->tp_name);
+        return NULL;
+    }
     Py_INCREF(obj);
     PyObject* res = PyIter_Next(obj);
     Py_DECREF(obj);