Issue #16230: Fix a crash in select.select() when one the lists changes size while...
authorAntoine Pitrou <solipsis@pitrou.net>
Thu, 1 Nov 2012 19:13:54 +0000 (20:13 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Thu, 1 Nov 2012 19:13:54 +0000 (20:13 +0100)
Patch by Serhiy Storchaka.

Lib/test/test_select.py
Misc/NEWS
Modules/selectmodule.c

index fe92f45145f8b7923c09cc09bf83a47730cb3394..4ddc2176cafbdfdddb0e073a5283ef131e4b4e40 100644 (file)
@@ -49,6 +49,16 @@ class SelectTestCase(unittest.TestCase):
             self.fail('Unexpected return values from select():', rfd, wfd, xfd)
         p.close()
 
+    # Issue 16230: Crash on select resized list
+    def test_select_mutated(self):
+        a = []
+        class F:
+            def fileno(self):
+                del a[-1]
+                return sys.__stdout__.fileno()
+        a[:] = [F()] * 10
+        self.assertEqual(select.select([], a, []), ([], a[:5], []))
+
 def test_main():
     support.run_unittest(SelectTestCase)
     support.reap_children()
index 7ece3120b8c168bdd594f60c55dffdafe3dab65e..4708e3f35a4bc9629254be63325b94d7a3f4a6eb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -143,6 +143,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #16230: Fix a crash in select.select() when one the lists changes
+  size while iterated on.  Patch by Serhiy Storchaka.
+
 - Issue #16228: Fix a crash in the json module where a list changes size
   while it is being encoded.  Patch by Serhiy Storchaka.
 
index 98b3108bf3ceb7c3c0eade9b5c79c8d7f29009aa..0736215972d7e30935610c1d353e6a315dd955ac 100644 (file)
@@ -83,7 +83,7 @@ seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
 {
     int max = -1;
     int index = 0;
-    Py_ssize_t i, len = -1;
+    Py_ssize_t i;
     PyObject* fast_seq = NULL;
     PyObject* o = NULL;
 
@@ -94,9 +94,7 @@ seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
     if (!fast_seq)
         return -1;
 
-    len = PySequence_Fast_GET_SIZE(fast_seq);
-
-    for (i = 0; i < len; i++)  {
+    for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++)  {
         SOCKET v;
 
         /* any intervening fileno() calls could decr this refcnt */