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.
This commit is contained in:
Benjamin Peterson 2019-09-12 00:43:22 +01:00 committed by GitHub
parent ee536b2020
commit 39de95b746
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View File

@ -0,0 +1,2 @@
Fix an off-by-one error in PyState_AddModule that could cause out-of-bounds
memory access.

View File

@ -684,7 +684,7 @@ _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
if (!state->modules_by_index) if (!state->modules_by_index)
return -1; return -1;
} }
while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) while (PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
if (PyList_Append(state->modules_by_index, Py_None) < 0) if (PyList_Append(state->modules_by_index, Py_None) < 0)
return -1; return -1;
Py_INCREF(module); Py_INCREF(module);
@ -702,14 +702,12 @@ PyState_AddModule(PyObject* module, struct PyModuleDef* def)
return -1; return -1;
} }
index = def->m_base.m_index; index = def->m_base.m_index;
if (state->modules_by_index) { if (state->modules_by_index &&
if(PyList_GET_SIZE(state->modules_by_index) >= index) { index < PyList_GET_SIZE(state->modules_by_index) &&
if(module == PyList_GET_ITEM(state->modules_by_index, index)) { module == PyList_GET_ITEM(state->modules_by_index, index)) {
Py_FatalError("PyState_AddModule: Module already added!"); Py_FatalError("PyState_AddModule: Module already added!");
return -1; return -1;
} }
}
}
return _PyState_AddModule(module, def); return _PyState_AddModule(module, def);
} }