Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-31675: Fix memory leaks in Tkinter's methods splitlist() and split() #3866

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 3, 2017

when pass a string larger than 2 GiB.

Decrease memory requirements for Tcl's bigmem tests.

https://bugs.python.org/issue31675

when pass a string larger than 2 GiB.

Decrease memory requirements for Tcl's bigmem tests.
@vstinner
Copy link
Member

vstinner commented Oct 3, 2017

I wrote a patch using "et#" PyArg format to prevent one strlen(), but I don't think that it's worth it. I expect that most proceeded strings are smaller than 100 characters, and strlen() is heavily optimized ;-)

diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 91d3dd583e..f8fee1a4ef 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -2220,7 +2220,8 @@ static PyObject *
 _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
 /*[clinic end generated code: output=13b51d34386d36fb input=2b2e13351e3c0b53]*/
 {
-    char *list;
+    char *list = NULL;
+    Py_ssize_t listlen;
     int argc;
     const char **argv;
     PyObject *v;
@@ -2254,10 +2255,18 @@ _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
         return PySequence_Tuple(arg);
     }
 
-    if (!PyArg_Parse(arg, "et:splitlist", "utf-8", &list))
+    if (!PyArg_Parse(arg, "et#:splitlist", "utf-8", &list, &listlen))
         return NULL;
 
-    CHECK_STRING_LENGTH(list);
+    if (listlen >= INT_MAX) {
+        PyErr_SetString(PyExc_OverflowError, "splitlist() string is too long");
+        return NULL;
+    }
+    if ((Py_ssize_t)strlen(list) != listlen) {
+        PyErr_SetString(PyExc_ValueError, "embedded null character");
+        return NULL;
+    }
+
     if (Tcl_SplitList(Tkapp_Interp(self), list,
                       &argc, &argv) == TCL_ERROR)  {
         PyMem_Free(list);

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@serhiy-storchaka
Copy link
Member Author

Before creating this PR I wrote a patch which got rid of "et" and creating a temporary copy on the heap. But I think it should be only in 3.7. As a bug fix I choose simpler solution.

@serhiy-storchaka serhiy-storchaka merged commit 27c623c into python:master Oct 3, 2017
@miss-islington
Copy link
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the tkinter-splitlist-leak branch October 3, 2017 19:40
@bedevere-bot
Copy link

GH-3874 is a backport of this pull request to the 3.6 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 3, 2017
…d split() (pythonGH-3866)

when pass a string larger than 2 GiB.

Decrease memory requirements for Tcl's bigmem tests.
(cherry picked from commit 27c623c)
@miss-islington
Copy link
Contributor

Sorry, @serhiy-storchaka, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 27c623c845dd6e4b8e1782666ca3a956636da266 2.7

serhiy-storchaka pushed a commit that referenced this pull request Oct 3, 2017
…d split() (GH-3866) (#3874)

when pass a string larger than 2 GiB.

Decrease memory requirements for Tcl's bigmem tests.
(cherry picked from commit 27c623c)
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Oct 3, 2017
…d split() (pythonGH-3866)

when pass a string larger than 2 GiB.

Decrease memory requirements for Tcl's bigmem tests..
(cherry picked from commit 27c623c)
@bedevere-bot
Copy link

GH-3876 is a backport of this pull request to the 2.7 branch.

serhiy-storchaka added a commit that referenced this pull request Oct 4, 2017
…d split() (GH-3866) (#3876)

when pass a string larger than 2 GiB.

Decrease memory requirements for Tcl's bigmem tests..
(cherry picked from commit 27c623c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants