From 0141d15aadcf80551242825116c43ab4acba2ab7 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 27 Jan 2014 23:10:49 -0800 Subject: [PATCH 1/2] Allocate storage for font_bytes, fixes #483 --- _imagingft.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/_imagingft.c b/_imagingft.c index d99bb91b2..6a646f64e 100644 --- a/_imagingft.c +++ b/_imagingft.c @@ -73,6 +73,7 @@ static FT_Library library; typedef struct { PyObject_HEAD FT_Face face; + unsigned char *font_bytes; } FontObject; static PyTypeObject Font_Type; @@ -101,7 +102,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) /* create a font object from a file name and a size (in pixels) */ FontObject* self; - int error; + int error = 0; char* filename = NULL; int size; @@ -120,11 +121,13 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) ); return NULL; } + if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|iss#", kwlist, Py_FileSystemDefaultEncoding, &filename, &size, &index, &encoding, &font_bytes, - &font_bytes_size)) + &font_bytes_size)) { return NULL; + } self = PyObject_New(FontObject, &Font_Type); if (!self) { @@ -136,7 +139,17 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) if (filename && font_bytes_size <= 0) { error = FT_New_Face(library, filename, index, &self->face); } else { - error = FT_New_Memory_Face(library, (FT_Byte*)font_bytes, font_bytes_size, index, &self->face); + /* need to have allocated storage for font_bytes for the life of the object.*/ + /* Don't free this before FT_Done_Face */ + self->font_bytes = PyMem_Malloc(font_bytes_size); + if (!self->font_bytes) { + error = 65; // Out of Memory in Freetype. + } + if (!error) { + memcpy(self->font_bytes, font_bytes, (size_t)font_bytes_size); + error = FT_New_Memory_Face(library, (FT_Byte*)self->font_bytes, + font_bytes_size, index, &self->face); + } } if (!error) @@ -152,6 +165,9 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) PyMem_Free(filename); if (error) { + if (self->font_bytes) { + PyMem_Free(self->font_bytes); + } PyObject_Del(self); return geterror(error); } @@ -435,6 +451,9 @@ static void font_dealloc(FontObject* self) { FT_Done_Face(self->face); + if (self->font_bytes) { + PyMem_Free(self->font_bytes); + } PyObject_Del(self); } From bd5935032f295a1ae78aae26b671eec8f915a84d Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 27 Jan 2014 23:24:42 -0800 Subject: [PATCH 2/2] initialize unused pointer to prevent freeing without allocating --- _imagingft.c | 1 + 1 file changed, 1 insertion(+) diff --git a/_imagingft.c b/_imagingft.c index 6a646f64e..eb6313704 100644 --- a/_imagingft.c +++ b/_imagingft.c @@ -137,6 +137,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) } if (filename && font_bytes_size <= 0) { + self->font_bytes = NULL; error = FT_New_Face(library, filename, index, &self->face); } else { /* need to have allocated storage for font_bytes for the life of the object.*/