Fix a potential crash in the FreeType bindings, if the string object passed in from python is deleted before the face oject.

This commit is contained in:
Kovid Goyal 2012-10-28 16:11:24 +05:30
parent 488b82b6ff
commit 6d355b82b8

View File

@ -22,6 +22,7 @@ typedef struct {
// ensure it is garbage collected before the library object, to prevent // ensure it is garbage collected before the library object, to prevent
// segfaults. // segfaults.
PyObject *library; PyObject *library;
PyObject *data;
} Face; } Face;
typedef struct { typedef struct {
@ -40,9 +41,12 @@ Face_dealloc(Face* self)
} }
self->face = NULL; self->face = NULL;
Py_DECREF(self->library); Py_XDECREF(self->library);
self->library = NULL; self->library = NULL;
Py_XDECREF(self->data);
self->data = NULL;
self->ob_type->tp_free((PyObject*)self); self->ob_type->tp_free((PyObject*)self);
} }
@ -55,8 +59,6 @@ Face_init(Face *self, PyObject *args, PyObject *kwds)
PyObject *ft; PyObject *ft;
if (!PyArg_ParseTuple(args, "Os#", &ft, &data, &sz)) return -1; if (!PyArg_ParseTuple(args, "Os#", &ft, &data, &sz)) return -1;
self->library = ft;
Py_XINCREF(ft);
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
error = FT_New_Memory_Face( ( (FreeType*)ft )->library, error = FT_New_Memory_Face( ( (FreeType*)ft )->library,
@ -70,6 +72,10 @@ Face_init(Face *self, PyObject *args, PyObject *kwds)
PyErr_Format(FreeTypeError, "Failed to initialize the Font with error: 0x%x", error); PyErr_Format(FreeTypeError, "Failed to initialize the Font with error: 0x%x", error);
return -1; return -1;
} }
self->library = ft;
Py_XINCREF(ft);
self->data = PySequence_GetItem(args, 1);
return 0; return 0;
} }