Skip to content

Commit f4cc216

Browse files
committed
Issue python#15394: Fix ref leaks in PyModule_Create.
Patch by Julia Lawall.
2 parents 77d3283 + 29e49d6 commit f4cc216

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ Amos Latteier
601601
Piers Lauder
602602
Ben Laurie
603603
Simon Law
604+
Julia Lawall
604605
Chris Lawrence
605606
Brian Leair
606607
Mathieu Leduc-Hamel

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15394: An issue in PyModule_Create that caused references to
14+
be leaked on some error paths has been fixed. Patch by Julia Lawall.
15+
1316
- Issue #15368: An issue that caused bytecode generation to be
1417
non-deterministic has been fixed.
1518

Objects/moduleobject.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,30 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version)
126126
d = PyModule_GetDict((PyObject*)m);
127127
if (module->m_methods != NULL) {
128128
n = PyUnicode_FromString(name);
129-
if (n == NULL)
129+
if (n == NULL) {
130+
Py_DECREF(m);
130131
return NULL;
132+
}
131133
for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
132134
if ((ml->ml_flags & METH_CLASS) ||
133135
(ml->ml_flags & METH_STATIC)) {
134136
PyErr_SetString(PyExc_ValueError,
135137
"module functions cannot set"
136138
" METH_CLASS or METH_STATIC");
137139
Py_DECREF(n);
140+
Py_DECREF(m);
138141
return NULL;
139142
}
140143
v = PyCFunction_NewEx(ml, (PyObject*)m, n);
141144
if (v == NULL) {
142145
Py_DECREF(n);
146+
Py_DECREF(m);
143147
return NULL;
144148
}
145149
if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
146150
Py_DECREF(v);
147151
Py_DECREF(n);
152+
Py_DECREF(m);
148153
return NULL;
149154
}
150155
Py_DECREF(v);
@@ -155,6 +160,7 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version)
155160
v = PyUnicode_FromString(module->m_doc);
156161
if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
157162
Py_XDECREF(v);
163+
Py_DECREF(m);
158164
return NULL;
159165
}
160166
Py_DECREF(v);

0 commit comments

Comments
 (0)