]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44608: Fix memory leak in _tkinter._flatten() (GH-27107)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 14 Jul 2021 05:44:08 +0000 (22:44 -0700)
committerGitHub <noreply@github.com>
Wed, 14 Jul 2021 05:44:08 +0000 (22:44 -0700)
if it is called with a sequence or set, but not list or tuple.
(cherry picked from commit f572cbf1faab33d9afbbe3e95738ed6fbe6e48e6)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_tcl.py
Misc/NEWS.d/next/Library/2021-07-13-09-01-33.bpo-44608.R3IcM1.rst [new file with mode: 0644]
Modules/_tkinter.c

index 0bbb39b147ea1e543afec9cb29dbd2bab97a0dd9..fa974d181136e73991d82e6b2c5a813847e93539 100644 (file)
@@ -41,8 +41,14 @@ def get_tk_patchlevel():
 class TkinterTest(unittest.TestCase):
 
     def testFlattenLen(self):
-        # flatten(<object with no length>)
+        # Object without length.
         self.assertRaises(TypeError, _tkinter._flatten, True)
+        # Object with length, but not sequence.
+        self.assertRaises(TypeError, _tkinter._flatten, {})
+        # Sequence or set, but not tuple or list.
+        # (issue44608: there were leaks in the following cases)
+        self.assertRaises(TypeError, _tkinter._flatten, 'string')
+        self.assertRaises(TypeError, _tkinter._flatten, {'set'})
 
 
 class TclTest(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2021-07-13-09-01-33.bpo-44608.R3IcM1.rst b/Misc/NEWS.d/next/Library/2021-07-13-09-01-33.bpo-44608.R3IcM1.rst
new file mode 100644 (file)
index 0000000..e0cf948
--- /dev/null
@@ -0,0 +1,2 @@
+Fix memory leak in :func:`_tkinter._flatten` if it is called with a sequence
+or set, but not list or tuple.
index b7958ea627b4c169fb44892baf667cc52ce4ec96..19853ce49118209421dc077ba38c512f8bb7051e 100644 (file)
@@ -3197,8 +3197,10 @@ _tkinter__flatten(PyObject *module, PyObject *item)
 
     context.size = 0;
 
-    if (!_flatten1(&context, item,0))
+    if (!_flatten1(&context, item, 0)) {
+        Py_XDECREF(context.tuple);
         return NULL;
+    }
 
     if (_PyTuple_Resize(&context.tuple, context.size))
         return NULL;