Klaus Demo ~jonashaag/bjoern / 73e552f
Fix segmentation fault in old interpreters (#173) * Fix segmentation fault in old interpreters Due to bug with constructing empty strings via PyString_FromFormat(), bjoern crashed Python interpreter. It happens on Python 2 interpreters before 2.7.16 and Python 3 interpreters before 3.5. https://bugs.python.org/issue33817 * Add comments about empty Python string creation Goldstein authored 10 months ago Jonas Haag committed 10 months ago
2 changed file(s) with 15 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
1414 #define _PEP3333_BytesLatin1_FromUnicode(u) PyUnicode_AsLatin1String(u)
1515 #define _PEP3333_String_FromUTF8String(data) PyUnicode_FromString(data)
1616 #define _PEP3333_String_FromLatin1StringAndSize(data, len) PyUnicode_DecodeLatin1(data, len, "replace")
17 // Shouldn't use FromFormat here because of Python bug:
18 // https://bugs.python.org/issue33817
19 // While problem with PyUnicode_FromString("") was not reported, it's still better
20 // to create empty string without formatting
21 #define _PEP3333_String_Empty() PyUnicode_FromString("")
1722 #define _PEP3333_String_FromFormat(...) PyUnicode_FromFormat(__VA_ARGS__)
1823 #define _PEP3333_String_GET_SIZE(u) PyUnicode_GET_LENGTH(u)
1924 #define _PEP3333_String_Concat(u1, u2) PyUnicode_Concat(u1, u2)
3035 #define _PEP3333_Bytes_Resize(bytes, len) _PyString_Resize(bytes, len)
3136 #define _PEP3333_BytesLatin1_FromUnicode(u) (Py_INCREF(u),u)
3237 #define _PEP3333_String_FromUTF8String(data) PyString_FromString(data) // Assume UTF8
38 // Can't use FromFormat here because of Python bug:
39 // https://bugs.python.org/issue33817
40 // Arguments (NULL, 0) will create empty string, it's explicitly allowed
41 #define _PEP3333_String_Empty() PyString_FromStringAndSize(NULL, 0)
3342 #define _PEP3333_String_FromFormat(...) PyString_FromFormat(__VA_ARGS__)
3443 #define _PEP3333_String_GET_SIZE(u) PyString_GET_SIZE(u)
3544
397397 PyDict_SetItemString(wsgi_base_dict, "SERVER_NAME", server_info->host);
398398
399399 if (server_info->port == Py_None) {
400 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", _PEP3333_String_FromFormat(""));
400 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", _PEP3333_String_Empty());
401401 } else {
402402 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", _PEP3333_String_FromFormat("%i", server_info->port));
403403 }
404404 } else {
405405 /* SERVER_NAME is required, but not usefull with UNIX type sockets */
406 PyDict_SetItemString(wsgi_base_dict, "SERVER_NAME", _PEP3333_String_FromFormat(""));
407 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", _PEP3333_String_FromFormat(""));
408 }
409 }
410 }
406 PyDict_SetItemString(wsgi_base_dict, "SERVER_NAME", _PEP3333_String_Empty());
407 PyDict_SetItemString(wsgi_base_dict, "SERVER_PORT", _PEP3333_String_Empty());
408 }
409 }
410 }