Add missing checks for failures in calls to PyUnicode_AsUTF8String.

Previously a seg fault could occur when passing invalid UTF8 strings (low
surrogates), eg passing u"\udcff" to the C layer (Python 3).
This commit is contained in:
William S Fulton 2017-12-04 18:41:55 +00:00
commit b0e29fbdf3
12 changed files with 92 additions and 25 deletions

View file

@ -1672,14 +1672,16 @@ SWIG_Python_AddErrMesg(const char* mesg, int infront)
PyObject *traceback = 0;
PyErr_Fetch(&type, &value, &traceback);
if (value) {
char *tmp;
PyObject *old_str = PyObject_Str(value);
const char *tmp = SWIG_Python_str_AsChar(old_str);
if (!tmp)
tmp = "Invalid error message";
Py_XINCREF(type);
PyErr_Clear();
if (infront) {
PyErr_Format(type, "%s %s", mesg, tmp = SWIG_Python_str_AsChar(old_str));
PyErr_Format(type, "%s %s", mesg, tmp);
} else {
PyErr_Format(type, "%s %s", tmp = SWIG_Python_str_AsChar(old_str), mesg);
PyErr_Format(type, "%s %s", tmp, mesg);
}
SWIG_Python_str_DelForPy3(tmp);
Py_DECREF(old_str);
@ -1805,6 +1807,8 @@ SWIG_Python_NonDynamicSetAttr(PyObject *obj, PyObject *name, PyObject *value) {
Py_INCREF(name);
} else {
encoded_name = PyUnicode_AsUTF8String(name);
if (!encoded_name)
return -1;
}
PyErr_Format(PyExc_AttributeError, "'%.100s' object has no attribute '%.200s'", tp->tp_name, PyString_AsString(encoded_name));
Py_DECREF(encoded_name);