Commit 68c3cc53 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

PyVTKObject: own the classname memory

In Python3, we take the actual object and construct a new bytes() object
from it. We then take a pointer to its data and decref the bytes object.
If the garbage collector runs between the decref and the use of the
classname variable further down, it is a use-after-free.
parent 1c54b47d
......@@ -486,14 +486,14 @@ PyObject *PyVTKObject_FromPointer(
{
// This will be set if we create a new C++ object
bool created = false;
const char *classname = vtkPythonUtil::StripModule(pytype->tp_name);
std::string classname = vtkPythonUtil::StripModule(pytype->tp_name);
PyVTKClass *cls = 0;
if (ptr)
{
// If constructing from an existing C++ object, use its actual class
classname = ptr->GetClassName();
cls = vtkPythonUtil::FindClass(classname);
cls = vtkPythonUtil::FindClass(classname.c_str());
}
if (cls == 0)
......@@ -510,14 +510,15 @@ PyObject *PyVTKObject_FromPointer(
s = tmp;
}
#endif
classname = PyBytes_AsString(s);
if (classname == 0)
const char *vtkname_classname = PyBytes_AsString(s);
if (vtkname_classname == 0)
{
Py_DECREF(s);
return NULL;
}
classname = vtkname_classname;
}
cls = vtkPythonUtil::FindClass(classname);
cls = vtkPythonUtil::FindClass(classname.c_str());
if (cls == 0)
{
PyErr_Format(PyExc_ValueError,
......@@ -549,7 +550,7 @@ PyObject *PyVTKObject_FromPointer(
// Check the type of the newly-created object
const char *newclassname = ptr->GetClassName();
if (strcmp(newclassname, classname) != 0)
if (newclassname == classname)
{
PyVTKClass *newclass = vtkPythonUtil::FindClass(newclassname);
if (newclass)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment