Skip to content

_threadmodule does not check for NULL after PyType_GetModuleByDef calls #114414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sobolevn opened this issue Jan 22, 2024 · 1 comment
Closed
Assignees
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Jan 22, 2024

Bug report

PyType_GetModuleByDef can return NULL:

cpython/Objects/typeobject.c

Lines 4640 to 4645 in fd49e22

PyErr_Format(
PyExc_TypeError,
"PyType_GetModuleByDef: No superclass of '%s' has the given module",
type->tp_name);
return NULL;
}

I think that right now this is a hypothetical issue, because in practice NULL will never be returned for these samples. But, all other similar places do check for NULL. I guess that adding a check won't hurt. And in the future, if something changes - we would be safe.

Samples:

PyObject *module = PyType_GetModuleByDef(type, &thread_module);
thread_module_state *state = get_thread_state(module);

PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module);
thread_module_state *state = get_thread_state(module);

PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module);
thread_module_state *state = get_thread_state(module);

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error extension-modules C modules in the Modules dir labels Jan 22, 2024
@sobolevn sobolevn self-assigned this Jan 22, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Jan 22, 2024
@erlend-aasland
Copy link
Contributor

For similar cases (retrieve module state in an extension module), we normally use assert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants