Skip to content

Commit b7e5a55

Browse files
Avoid possible memory leak in tkinter.c on Windows.
1 parent 1daad8a commit b7e5a55

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
@@ -128,18 +128,20 @@ _get_tcl_lib_path(void)
128128
}
129129

130130
/* Check expected location for an installed Python first */
131-
tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
132-
if (tcl_library_path == NULL) {
131+
PyObject* tmp_tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
132+
if (tmp_tcl_library_path == NULL) {
133133
Py_DECREF(prefix);
134134
return NULL;
135135
}
136-
tcl_library_path = PyUnicode_Concat(prefix, tcl_library_path);
136+
tcl_library_path = PyUnicode_Concat(prefix, tmp_tcl_library_path);
137+
Py_DECREF(tmp_tcl_library_path);
137138
Py_DECREF(prefix);
138139
if (tcl_library_path == NULL) {
139140
return NULL;
140141
}
141142
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
142143
if (stat_return_value == -2) {
144+
Py_DECREF(tcl_library_path);
143145
return NULL;
144146
}
145147
if (stat_return_value == -1) {
@@ -154,16 +156,17 @@ _get_tcl_lib_path(void)
154156
}
155157
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
156158
if (stat_return_value == -2) {
159+
Py_DECREF(tcl_library_path);
157160
return NULL;
158161
}
159162
if (stat_return_value == -1) {
160163
/* tcltkDir for a repository build doesn't exist either,
161164
reset errno and leave Tcl to its own devices */
162165
errno = 0;
163-
tcl_library_path = NULL;
166+
Py_CLEAR(tcl_library_path);
164167
}
165168
#else
166-
tcl_library_path = NULL;
169+
Py_CLEAR(tcl_library_path);
167170
#endif
168171
}
169172
already_checked = 1;
@@ -707,11 +710,13 @@ Tkapp_New(const char *screenName, const char *className,
707710
if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
708711
str_path = _get_tcl_lib_path();
709712
if (str_path == NULL && PyErr_Occurred()) {
713+
Py_DECREF(v);
710714
return NULL;
711715
}
712716
if (str_path != NULL) {
713717
utf8_path = PyUnicode_AsUTF8String(str_path);
714718
if (utf8_path == NULL) {
719+
Py_DECREF(v);
715720
return NULL;
716721
}
717722
Tcl_SetVar(v->interp,

0 commit comments

Comments
 (0)