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