Skip to content

Commit e812fe9

Browse files
[3.13] gh-151337: Avoid possible memory leak in _tkinter.c on Windows. (GH-151340) (GH-151381)
(cherry picked from commit 71805db) Co-authored-by: Ivy Xu <fakeshadow1337@gmail.com>
1 parent 5bc2072 commit e812fe9

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid possible memory leak in ``tkinter.c`` on Windows.

Modules/_tkinter.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,20 @@ _get_tcl_lib_path(void)
151151
}
152152

153153
/* Check expected location for an installed Python first */
154-
tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
155-
if (tcl_library_path == NULL) {
154+
PyObject* tmp_tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
155+
if (tmp_tcl_library_path == NULL) {
156156
Py_DECREF(prefix);
157157
return NULL;
158158
}
159-
tcl_library_path = PyUnicode_Concat(prefix, tcl_library_path);
159+
tcl_library_path = PyUnicode_Concat(prefix, tmp_tcl_library_path);
160+
Py_DECREF(tmp_tcl_library_path);
160161
Py_DECREF(prefix);
161162
if (tcl_library_path == NULL) {
162163
return NULL;
163164
}
164165
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
165166
if (stat_return_value == -2) {
167+
Py_DECREF(tcl_library_path);
166168
return NULL;
167169
}
168170
if (stat_return_value == -1) {
@@ -177,16 +179,17 @@ _get_tcl_lib_path(void)
177179
}
178180
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
179181
if (stat_return_value == -2) {
182+
Py_DECREF(tcl_library_path);
180183
return NULL;
181184
}
182185
if (stat_return_value == -1) {
183186
/* tcltkDir for a repository build doesn't exist either,
184187
reset errno and leave Tcl to its own devices */
185188
errno = 0;
186-
tcl_library_path = NULL;
189+
Py_CLEAR(tcl_library_path);
187190
}
188191
#else
189-
tcl_library_path = NULL;
192+
Py_CLEAR(tcl_library_path);
190193
#endif
191194
}
192195
already_checked = 1;
@@ -718,11 +721,13 @@ Tkapp_New(const char *screenName, const char *className,
718721
if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
719722
str_path = _get_tcl_lib_path();
720723
if (str_path == NULL && PyErr_Occurred()) {
724+
Py_DECREF(v);
721725
return NULL;
722726
}
723727
if (str_path != NULL) {
724728
utf8_path = PyUnicode_AsUTF8String(str_path);
725729
if (utf8_path == NULL) {
730+
Py_DECREF(v);
726731
return NULL;
727732
}
728733
Tcl_SetVar(v->interp,

0 commit comments

Comments
 (0)