8000 closes bpo-38124: Fix bounds check in PyState_AddModule. (GH-16007) · python/cpython@39de95b · GitHub
[go: up one dir, main page]

Skip to content

Commit 39de95b

Browse files
authored
closes bpo-38124: Fix bounds check in PyState_AddModule. (GH-16007)
The >=, checking whether a module index was in already in the module-by-index list, needed to be strict. Also, fold nested ifs into one and fix some bad spacing.
1 parent ee536b2 commit 39de95b

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an off-by-one error in PyState_AddModule that could cause out-of-bounds
2+
memory access.

Python/pystate.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
684684
if (!state->modules_by_index)
685685
return -1;
686686
}
687-
while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
687+
while (PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
688688
if (PyList_Append(state->modules_by_index, Py_None) < 0)
689689
return -1;
690690
Py_INCREF(module);
@@ -702,13 +702,11 @@ PyState_AddModule(PyObject* module, struct PyModuleDef* def)
702702
return -1;
703703
}
704704
index = def->m_base.m_index;
705-
if (state->modules_by_index) {
706-
if(PyList_GET_SIZE(state->modules_by_index) >= index) {
707-
if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
708-
Py_FatalError("PyState_AddModule: Module already added!");
709-
return -1;
710-
}
711-
}
705+
if (state->modules_by_index &&
706+
index < PyList_GET_SIZE(state->modules_by_index) &&
707+
module == PyList_GET_ITEM(state->modules_by_index, index)) {
708+
Py_FatalError("PyState_AddModule: Module already added!");
709+
return -1;
712710
}
713711
return _PyState_AddModule(module, def);
714712
}

0 commit comments

Comments
 (0)
0