[issue36386] segfault on PyUnicode_DecodeFSDefaultAndSize for uninitialized Py

STINNER Victor report at bugs.python.org
Thu Mar 21 05:19:43 EDT 2019


STINNER Victor <vstinner at redhat.com> added the comment:

> I'm expecting a "this is not a bug, why would the interpreter not be initialized",

this is not a bug, why would the interpreter not be initialized, as documented at:
https://docs.python.org/dev/c-api/init.html

> but it would be nice to get a friendly error message since this is a public API.
IF so, am also happy to submit a PR with a fix

Python has a *very large* C API. It doesn't seem worth it to me to modify every single Python function to detect when the API is misused.

There is maybe room for some clever tricks for some specific cases. For example, modify PyMem_Malloc and PyObject_Malloc default allocators to have an implementation with calls Py_FatalError() with a clear error message like "Py_Initialize must be called before using the Python C API". I modified Python internals to only use PyMem_RawMalloc during early Python initialization, and we can reconfigure PyMem_Malloc and PyObject_Malloc during Py_Initialize().

But... according to your backtrace, it wouldn't help for your exact use case: call PyUnicode_DecodeFSDefault() before Py_Initialize(). The crash occurs before PyMem_Malloc or PyObject_Malloc is called. I don't see any other clever tricks which would have no impact on performance when the API is properly used.


... I suggest to close this issue.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36386>
_______________________________________


More information about the Python-bugs-list mailing list