summaryrefslogtreecommitdiffstats
path: root/Objects/moduleobject.c
diff options
context:
space:
mode:
authorMarcel Plch <gmarcel.plch@gmail.com>2018-03-17 05:41:20 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2018-03-17 05:41:20 (GMT)
commitc2b0b12d1a137ada1023ab7c10b8d9a0249d95f9 (patch)
tree53b82a27d468a5fc63067d3d1ecbd186388666f9 /Objects/moduleobject.c
parentd6e140466142018ddbb7541185348be2b833a2ce (diff)
downloadcpython-c2b0b12d1a137ada1023ab7c10b8d9a0249d95f9.zip
cpython-c2b0b12d1a137ada1023ab7c10b8d9a0249d95f9.tar.gz
cpython-c2b0b12d1a137ada1023ab7c10b8d9a0249d95f9.tar.bz2
bpo-32374: m_traverse may be called with m_state=NULL (GH-5140)
Multi-phase initialized modules allow m_traverse to be called while the module is still being initialized, so module authors may need to account for that.
Diffstat (limited to 'Objects/moduleobject.c')
-rw-r--r--Objects/moduleobject.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index d6cde40..8fb368e 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -21,6 +21,17 @@ static PyMemberDef module_members[] = {
{0}
};
+
+/* Helper for sanity check for traverse not handling m_state == NULL
+ * Issue #32374 */
+#ifdef Py_DEBUG
+static int
+bad_traverse_test(PyObject *self, void *arg) {
+ assert(self != NULL);
+ return 0;
+}
+#endif
+
PyTypeObject PyModuleDef_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"moduledef", /* tp_name */
@@ -345,6 +356,16 @@ PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api
}
}
+ /* Sanity check for traverse not handling m_state == NULL
+ * This doesn't catch all possible cases, but in many cases it should
+ * make many cases of invalid code crash or raise Valgrind issues
+ * sooner than they would otherwise.
+ * Issue #32374 */
+#ifdef Py_DEBUG
+ if (def->m_traverse != NULL) {
+ def->m_traverse(m, bad_traverse_test, NULL);
+ }
+#endif
Py_DECREF(nameobj);
return m;