fix possible crashes in Tools::escapedUnicodeFromUtf8

This commit is contained in:
wmayer 2016-09-14 15:18:19 +02:00
parent 796c8330fe
commit fb9fb9873f

View File

@ -30,6 +30,7 @@
# include <QTime> # include <QTime>
#include "PyExport.h" #include "PyExport.h"
#include "Interpreter.h"
#include "Tools.h" #include "Tools.h"
namespace Base { namespace Base {
@ -144,17 +145,28 @@ std::string Base::Tools::narrow(const std::wstring& str)
std::string Base::Tools::escapedUnicodeFromUtf8(const char *s) std::string Base::Tools::escapedUnicodeFromUtf8(const char *s)
{ {
Base::PyGILStateLocker lock;
std::string escapedstr;
PyObject* unicode = PyUnicode_FromString(s); PyObject* unicode = PyUnicode_FromString(s);
if (!unicode)
return escapedstr;
PyObject* escaped = PyUnicode_AsUnicodeEscapeString(unicode); PyObject* escaped = PyUnicode_AsUnicodeEscapeString(unicode);
Py_DECREF(unicode); if (escaped) {
std::string escapedstr = std::string(PyString_AsString(escaped)); escapedstr = std::string(PyString_AsString(escaped));
Py_DECREF(escaped); Py_DECREF(escaped);
}
Py_DECREF(unicode);
return escapedstr; return escapedstr;
} }
std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s) std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s)
{ {
Base::PyGILStateLocker lock;
std::string string; std::string string;
PyObject* unicode = PyUnicode_DecodeUnicodeEscape(s.c_str(), s.size(), "strict"); PyObject* unicode = PyUnicode_DecodeUnicodeEscape(s.c_str(), s.size(), "strict");
if (!unicode) if (!unicode)
return string; return string;