Skip to content

Commit

Permalink
fix possible crashes in Tools::escapedUnicodeFromUtf8
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Sep 14, 2016
1 parent 796c833 commit fb9fb98
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Base/Tools.cpp
Expand Up @@ -30,6 +30,7 @@

# include <QTime>
#include "PyExport.h"
#include "Interpreter.h"
#include "Tools.h"

namespace Base {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fb9fb98

Please sign in to comment.