Skip to content

Commit 639945f

Browse files
ByteFlowing1337miss-islington
authored andcommitted
gh-151337: Avoid possible memory leak in _tkinter.c on Windows. (GH-151340)
(cherry picked from commit 71805db) Co-authored-by: Ivy Xu <fakeshadow1337@gmail.com>
1 parent 7b62ea5 commit 639945f

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
@@ -152,18 +152,20 @@ _get_tcl_lib_path(void)
152152
}
153153

154154
/* Check expected location for an installed Python first */
155-
tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
156-
if (tcl_library_path == NULL) {
155+
PyObject* tmp_tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
156+
if (tmp_tcl_library_path == NULL) {
157157
Py_DECREF(prefix);
158158
return NULL;
159159
}
160-
tcl_library_path = PyUnicode_Concat(prefix, tcl_library_path);
160+
tcl_library_path = PyUnicode_Concat(prefix, tmp_tcl_library_path);
161+
Py_DECREF(tmp_tcl_library_path);
161162
Py_DECREF(prefix);
162163
if (tcl_library_path == NULL) {
163164
return NULL;
164165
}
165166
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
166167
if (stat_return_value == -2) {
168+
Py_DECREF(tcl_library_path);
167169
return NULL;
168170
}
169171
if (stat_return_value == -1) {
@@ -178,16 +180,17 @@ _get_tcl_lib_path(void)
178180
}
179181
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
180182
if (stat_return_value == -2) {
183+
Py_DECREF(tcl_library_path);
181184
return NULL;
182185
}
183186
if (stat_return_value == -1) {
184187
/* tcltkDir for a repository build doesn't exist either,
185188
reset errno and leave Tcl to its own devices */
186189
errno = 0;
187-
tcl_library_path = NULL;
190+
Py_CLEAR(tcl_library_path);
188191
}
189192
#else
190-
tcl_library_path = NULL;
193+
Py_CLEAR(tcl_library_path);
191194
#endif
192195
}
193196
already_checked = 1;
@@ -732,11 +735,13 @@ Tkapp_New(const char *screenName, const char *className,
732735
if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
733736
str_path = _get_tcl_lib_path();
734737
if (str_path == NULL && PyErr_Occurred()) {
738+
Py_DECREF(v);
735739
return NULL;
736740
}
737741
if (str_path != NULL) {
738742
utf8_path = PyUnicode_AsUTF8String(str_path);
739743
if (utf8_path == NULL) {
744+
Py_DECREF(v);
740745
return NULL;
741746
}
742747
Tcl_SetVar(v->interp,

0 commit comments

Comments
 (0)