]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44608: Fix memory leak in _tkinter._flatten() (GH-27107)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 14 Jul 2021 05:19:18 +0000 (08:19 +0300)
committerGitHub <noreply@github.com>
Wed, 14 Jul 2021 05:19:18 +0000 (08:19 +0300)
if it is called with a sequence or set, but not list or tuple.

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 e7a60db777820220229ea5b2e8c9d361607d2327..6e5ef097c888f967b60b38fce40ac5404b5c5892 100644 (file)
@@ -43,8 +43,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 14101d9e3951c06b23bad3a62b9b9d1345f8f9e9..329b291729d581b1fcef17a59c629e3ada288f5c 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;