From 5e159c2ff45aedf258a217078e8395886373fb6d Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Wed, 4 Oct 2017 10:21:13 +0000 Subject: [PATCH 1/4] tabs->spaces --- _imagingft.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/_imagingft.c b/_imagingft.c index afacfada9..3f005f029 100644 --- a/_imagingft.c +++ b/_imagingft.c @@ -225,11 +225,11 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, if (PyUnicode_Check(string)) { Py_UNICODE *text = PyUnicode_AS_UNICODE(string); Py_ssize_t size = PyUnicode_GET_SIZE(string); - if (! size) { - /* return 0 and clean up, no glyphs==no size, - and raqm fails with empty strings */ - goto failed; - } + if (! size) { + /* return 0 and clean up, no glyphs==no size, + and raqm fails with empty strings */ + goto failed; + } if (!raqm_set_text(rq, (const uint32_t *)(text), size)) { PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed"); goto failed; @@ -239,9 +239,9 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, else if (PyString_Check(string)) { char *text = PyString_AS_STRING(string); int size = PyString_GET_SIZE(string); - if (! size) { - goto failed; - } + if (! size) { + goto failed; + } if (!raqm_set_text_utf8(rq, text, size)) { PyErr_SetString(PyExc_ValueError, "raqm_set_text_utf8() failed"); goto failed; From 87abdf5e6d0627fd10f54b5a14e7f45ab05f7891 Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Wed, 4 Oct 2017 10:56:20 +0000 Subject: [PATCH 2/4] fix for #2783, Return none without error set --- _imaging.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/_imaging.c b/_imaging.c index ee2c3e897..ab0ffd916 100644 --- a/_imaging.c +++ b/_imaging.c @@ -2212,16 +2212,15 @@ void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){ PyBytes_AsStringAndSize(encoded_string, &buffer, &len); } - if (len) { - *text = calloc(len,1); - if (*text) { - memcpy(*text, buffer, len); - } - if(bytes) { - Py_DECREF(bytes); - } - return; + *text = calloc(len,1); + if (*text) { + memcpy(*text, buffer, len); } + if(bytes) { + Py_DECREF(bytes); + } + + return; #if PY_VERSION_HEX < 0x03000000 From 0454cb8eb9afb78c6c37cfaea734ef40433c9cda Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Wed, 4 Oct 2017 11:08:41 +0000 Subject: [PATCH 3/4] additional potential null returns without error set --- _imaging.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/_imaging.c b/_imaging.c index ab0ffd916..ec90e1f7f 100644 --- a/_imaging.c +++ b/_imaging.c @@ -2260,12 +2260,14 @@ _font_getmask(ImagingFontObject* self, PyObject* args) _font_text_asBytes(encoded_string, &text); if (!text) { + ImagingError_MemoryError(); return NULL; } im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize); if (!im) { free(text); + ImagingError_MemoryError(); return NULL; } @@ -2297,7 +2299,7 @@ _font_getmask(ImagingFontObject* self, PyObject* args) failed: free(text); ImagingDelete(im); - return NULL; + Py_RETURN_NONE; } static PyObject* @@ -2312,6 +2314,7 @@ _font_getsize(ImagingFontObject* self, PyObject* args) _font_text_asBytes(encoded_string, &text); if (!text) { + ImagingError_MemoryError(); return NULL; } From 5524d806b49b4eb5b03f5a8489c4cc2cb483095f Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Oct 2017 09:54:53 +0300 Subject: [PATCH 4/4] Failing test for #2783 --- Tests/test_imagedraw.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index e4bcb9faa..a79a75ca0 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -380,7 +380,6 @@ class TestImageDraw(PillowTestCase): self.assert_image_equal( im, Image.open("Tests/images/imagedraw_floodfill2.png")) - def test_floodfill_thresh(self): # floodfill() is experimental @@ -560,6 +559,19 @@ class TestImageDraw(PillowTestCase): # Assert self.assert_image_similar(im, Image.open(expected), 1) + def test_textsize_empty_string(self): + # https://github.com/python-pillow/Pillow/issues/2783 + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw.Draw(im) + + # Act + # Should not cause 'SystemError: returned NULL without setting an error' + draw.textsize("") + draw.textsize("\n") + draw.textsize("test\n") + if __name__ == '__main__': unittest.main()