From 328b5d0e64798cf17360c6b2a07c2b18f2798b88 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 4 Oct 2017 21:37:53 +0300 Subject: [PATCH] [2.7] bpo-31675: Fix memory leaks in Tkinter's methods splitlist() and split() (GH-3866) (#3876) when pass a string larger than 2 GiB. Decrease memory requirements for Tcl's bigmem tests.. (cherry picked from commit 27c623c845dd6e4b8e1782666ca3a956636da266) --- Lib/test/test_tcl.py | 38 +++++++++---------- .../2017-10-03-15-06-24.bpo-31675.Nh7jJ3.rst | 2 + Modules/_tkinter.c | 12 +++++- 3 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2017-10-03-15-06-24.bpo-31675.Nh7jJ3.rst diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py index 921d094d5a7..f0c9877862e 100644 --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -705,25 +705,25 @@ class BigmemTclTest(unittest.TestCase): self.check_huge_string_builtins(value) def check_huge_string_builtins(self, value): - self.assertRaises(OverflowError, self.interp.tk.getint, value) - self.assertRaises(OverflowError, self.interp.tk.getdouble, value) - self.assertRaises(OverflowError, self.interp.tk.getboolean, value) - self.assertRaises(OverflowError, self.interp.eval, value) - self.assertRaises(OverflowError, self.interp.evalfile, value) - self.assertRaises(OverflowError, self.interp.record, value) - self.assertRaises(OverflowError, self.interp.adderrorinfo, value) - self.assertRaises(OverflowError, self.interp.setvar, value, 'x', 'a') - self.assertRaises(OverflowError, self.interp.setvar, 'x', value, 'a') - self.assertRaises(OverflowError, self.interp.unsetvar, value) - self.assertRaises(OverflowError, self.interp.unsetvar, 'x', value) - self.assertRaises(OverflowError, self.interp.adderrorinfo, value) - self.assertRaises(OverflowError, self.interp.exprstring, value) - self.assertRaises(OverflowError, self.interp.exprlong, value) - self.assertRaises(OverflowError, self.interp.exprboolean, value) - self.assertRaises(OverflowError, self.interp.splitlist, value) - self.assertRaises(OverflowError, self.interp.split, value) - self.assertRaises(OverflowError, self.interp.createcommand, value, max) - self.assertRaises(OverflowError, self.interp.deletecommand, value) + tk = self.interp.tk + self.assertRaises(OverflowError, tk.getint, value) + self.assertRaises(OverflowError, tk.getdouble, value) + self.assertRaises(OverflowError, tk.getboolean, value) + self.assertRaises(OverflowError, tk.eval, value) + self.assertRaises(OverflowError, tk.evalfile, value) + self.assertRaises(OverflowError, tk.record, value) + self.assertRaises(OverflowError, tk.adderrorinfo, value) + self.assertRaises(OverflowError, tk.setvar, value, 'x', 'a') + self.assertRaises(OverflowError, tk.setvar, 'x', value, 'a') + self.assertRaises(OverflowError, tk.unsetvar, value) + self.assertRaises(OverflowError, tk.unsetvar, 'x', value) + self.assertRaises(OverflowError, tk.exprstring, value) + self.assertRaises(OverflowError, tk.exprlong, value) + self.assertRaises(OverflowError, tk.exprboolean, value) + self.assertRaises(OverflowError, tk.splitlist, value) + self.assertRaises(OverflowError, tk.split, value) + self.assertRaises(OverflowError, tk.createcommand, value, max) + self.assertRaises(OverflowError, tk.deletecommand, value) def setUpModule(): diff --git a/Misc/NEWS.d/next/Library/2017-10-03-15-06-24.bpo-31675.Nh7jJ3.rst b/Misc/NEWS.d/next/Library/2017-10-03-15-06-24.bpo-31675.Nh7jJ3.rst new file mode 100644 index 00000000000..4e4430773af --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-10-03-15-06-24.bpo-31675.Nh7jJ3.rst @@ -0,0 +1,2 @@ +Fixed memory leaks in Tkinter's methods splitlist() and split() when pass a +string larger than 2 GiB. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 6053e4bde44..444c268c0b4 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -2332,7 +2332,11 @@ Tkapp_SplitList(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list)) return NULL; - CHECK_STRING_LENGTH(list); + if (strlen(list) >= INT_MAX) { + PyErr_SetString(PyExc_OverflowError, "string is too long"); + PyMem_Free(list); + return NULL; + } if (Tcl_SplitList(Tkapp_Interp(self), list, &argc, &argv) == TCL_ERROR) { PyMem_Free(list); @@ -2394,7 +2398,11 @@ Tkapp_Split(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list)) return NULL; - CHECK_STRING_LENGTH(list); + if (strlen(list) >= INT_MAX) { + PyErr_SetString(PyExc_OverflowError, "string is too long"); + PyMem_Free(list); + return NULL; + } v = Split(list); PyMem_Free(list); return v;