This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Segmentation fault when using PyUnicode_FromString
Type: crash Stage: resolved
Components: Extension Modules Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: lazka, noxdafox, xiang.zhang
Priority: normal Keywords:

Created on 2016-12-21 19:52 by noxdafox, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg283777 - (view) Author: Matteo Cafasso (noxdafox) Date: 2016-12-21 19:52
The following code snippet:

----------------------------------------------
#include <stdio.h>
#include <Python.h>

int main()
{
    char *broken_string[8];
    char broken_char = 4294967252;

    sprintf(broken_string, "%c", broken_char);

    PyUnicode_FromString(broken_string);
}
----------------------------------------------

Produces a Segmentation Fault.

Is this behaviour the expected one? 

The real life example comes when reading a malformed path on a Ext4 filesystem. The read string causes PyUnicode_FromString to segfault.
msg283787 - (view) Author: Christoph Reiter (lazka) * Date: 2016-12-21 21:54
You're missing a call to Py_Initialize() [0]

[0] https://docs.python.org/3/c-api/init.html#c.Py_Initialize
msg283806 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-12-22 04:02
Just as Christoph said, you need to initialize first.
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73225
2016-12-22 04:02:55xiang.zhangsetstatus: open -> closed

nosy: + xiang.zhang
messages: + msg283806

resolution: not a bug
stage: resolved
2016-12-21 21:54:26lazkasetnosy: + lazka
messages: + msg283787
2016-12-21 19:52:12noxdafoxcreate