Fix/test for #2826, unchecked exception leading to null pointer dereference, segfault

This commit is contained in:
Eric Soroos 2017-11-02 20:46:17 +00:00
parent 7e484eba77
commit 3f4abf6caa
2 changed files with 17 additions and 6 deletions

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from helper import unittest, PillowTestCase from helper import unittest, PillowTestCase
from PIL import Image, ImageDraw, ImageFont, features from PIL import Image, ImageDraw, ImageFont, features
@ -409,7 +410,12 @@ class TestImageFont(PillowTestCase):
draw.text((10, 10), '', font=font) draw.text((10, 10), '', font=font)
self.assert_image_equal(im, target) self.assert_image_equal(im, target)
def test_unicode_pilfont(self):
# should not segfault, should return UnicodeDecodeError
# issue #2826
font = ImageFont.load_default()
with self.assertRaises(UnicodeEncodeError):
font.getsize(u"")
def _test_fake_loading_font(self, path_to_fake, fontname): def _test_fake_loading_font(self, path_to_fake, fontname):

View File

@ -2207,6 +2207,9 @@ void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){
if (PyUnicode_CheckExact(encoded_string)){ if (PyUnicode_CheckExact(encoded_string)){
bytes = PyUnicode_AsLatin1String(encoded_string); bytes = PyUnicode_AsLatin1String(encoded_string);
if (!bytes) {
return;
}
PyBytes_AsStringAndSize(bytes, &buffer, &len); PyBytes_AsStringAndSize(bytes, &buffer, &len);
} else if (PyBytes_Check(encoded_string)) { } else if (PyBytes_Check(encoded_string)) {
PyBytes_AsStringAndSize(encoded_string, &buffer, &len); PyBytes_AsStringAndSize(encoded_string, &buffer, &len);
@ -2215,13 +2218,15 @@ void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){
*text = calloc(len+1,1); *text = calloc(len+1,1);
if (*text) { if (*text) {
memcpy(*text, buffer, len); memcpy(*text, buffer, len);
} else {
ImagingError_MemoryError();
} }
if(bytes) { if (bytes) {
Py_DECREF(bytes); Py_DECREF(bytes);
} }
return; return;
/* UNDONE not reachable code */
#if PY_VERSION_HEX < 0x03000000 #if PY_VERSION_HEX < 0x03000000
/* likely case here is py2.x with an ordinary string. /* likely case here is py2.x with an ordinary string.
@ -2231,6 +2236,8 @@ void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){
*text = calloc(len,1); *text = calloc(len,1);
if (*text) { if (*text) {
memcpy(*text, buffer, len+1); memcpy(*text, buffer, len+1);
} else {
ImagingError_MemoryError();
} }
return; return;
} }
@ -2260,7 +2267,6 @@ _font_getmask(ImagingFontObject* self, PyObject* args)
_font_text_asBytes(encoded_string, &text); _font_text_asBytes(encoded_string, &text);
if (!text) { if (!text) {
ImagingError_MemoryError();
return NULL; return NULL;
} }
@ -2314,7 +2320,6 @@ _font_getsize(ImagingFontObject* self, PyObject* args)
_font_text_asBytes(encoded_string, &text); _font_text_asBytes(encoded_string, &text);
if (!text) { if (!text) {
ImagingError_MemoryError();
return NULL; return NULL;
} }