You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Segmentation fault with invalid Unicode command-line arguments in embedded Python #69817
This segmentation fault is present in Python 3.4, 3.5, and the latest development branch I downloaded, but is not present in Python 3.2. This program is obviously invalid and it may be reasonable to emit a fatal error in this situation, but it should not give a segmentation fault.
I believe the issue is that this codes leads to exception being thrown before exceptions are initialized, and more specifically, a call to PyExceptionClass_Check() within PyErr_Object() reads a NULL pointer. I haven't tested this but I expect that this problem would not appear when calling Python directly since Python sanitizes the command line arguments from main(). Nonetheless even here the possibility of other exceptions being raised early in the initialization sequence remains a potential problem.
The interpreter isn't initialized, so calling PyErr_Format in a release build segfaults when it tries to dereference a NULL PyThreadState. OTOH, a debug build should call PyThreadState_Get, which in this case calls Py_FatalError and aborts the process. Unfortunately 3.5.0+ debug builds don't call PyThreadState_Get due to the fix for bpo-25150.
the possibility of other exceptions being raised early in the
initialization sequence remains a potential problem.
PEP-432 proposes a pre-initialization phase that sets a valid Python thread state.
This bug has been fixed in the implementation of the PEP-587.
$ gcc x.c -l python3.8 $(pkg-config python-3.8 --cflags --libs) -o x
$ ./x
Fatal Python error: pyinit_main: can't finish initializing sysValueError: character U+110000 is not in range [U+0000; U+10ffff]
Current thread 0x00007fd23364e6c0 (most recent call first):
=> it doesn't crash anymore, but write a error message and exit.
Activity
itaibn commentedon Nov 16, 2015
The following embedded application, which calls Py_Main with a "-W X" argument where X is not a valid Unicode string, returns a segmentation fault:
This segmentation fault is present in Python 3.4, 3.5, and the latest development branch I downloaded, but is not present in Python 3.2. This program is obviously invalid and it may be reasonable to emit a fatal error in this situation, but it should not give a segmentation fault.
I believe the issue is that this codes leads to exception being thrown before exceptions are initialized, and more specifically, a call to PyExceptionClass_Check() within PyErr_Object() reads a NULL pointer. I haven't tested this but I expect that this problem would not appear when calling Python directly since Python sanitizes the command line arguments from main(). Nonetheless even here the possibility of other exceptions being raised early in the initialization sequence remains a potential problem.
eryksun commentedon Nov 16, 2015
The interpreter isn't initialized, so calling PyErr_Format in a release build segfaults when it tries to dereference a NULL PyThreadState. OTOH, a debug build should call PyThreadState_Get, which in this case calls Py_FatalError and aborts the process. Unfortunately 3.5.0+ debug builds don't call PyThreadState_Get due to the fix for bpo-25150.
PEP-432 proposes a pre-initialization phase that sets a valid Python thread state.
vstinner commentedon Sep 26, 2019
This bug has been fixed in the implementation of the PEP-587.
Current thread 0x00007fd23364e6c0 (most recent call first):
=> it doesn't crash anymore, but write a error message and exit.