Skip to content

Commit 29e49d6

Browse files
committed
Issue #15394: Fix ref leaks in PyModule_Create.
Patch by Julia Lawall.
1 parent 60c2266 commit 29e49d6

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ Soren Larsen
542542
Piers Lauder
543543
Ben Laurie
544544
Simon Law
545+
Julia Lawall
545546
Chris Lawrence
546547
Brian Leair
547548
James Lee

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.2.4
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 when using randomized hashing (-R) has been fixed.
1518

Objects/moduleobject.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,30 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version)
117117
d = PyModule_GetDict((PyObject*)m);
118118
if (module->m_methods != NULL) {
119119
n = PyUnicode_FromString(name);
120-
if (n == NULL)
120+
if (n == NULL) {
121+
Py_DECREF(m);
121122
return NULL;
123+
}
122124
for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
123125
if ((ml->ml_flags & METH_CLASS) ||
124126
(ml->ml_flags & METH_STATIC)) {
125127
PyErr_SetString(PyExc_ValueError,
126128
"module functions cannot set"
127129
" METH_CLASS or METH_STATIC");
128130
Py_DECREF(n);
131+
Py_DECREF(m);
129132
return NULL;
130133
}
131134
v = PyCFunction_NewEx(ml, (PyObject*)m, n);
132135
if (v == NULL) {
133136
Py_DECREF(n);
137+
Py_DECREF(m);
134138
return NULL;
135139
}
136140
if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
137141
Py_DECREF(v);
138142
Py_DECREF(n);
143+
Py_DECREF(m);
139144
return NULL;
140145
}
141146
Py_DECREF(v);
@@ -146,6 +151,7 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version)
146151
v = PyUnicode_FromString(module->m_doc);
147152
if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
148153
Py_XDECREF(v);
154+
Py_DECREF(m);
149155
return NULL;
150156
}
151157
Py_DECREF(v);

0 commit comments

Comments
 (0)