Merge pull request #2131 from wiredfool/imagecms-segfault

Fix for ImageCms Segfault
This commit is contained in:
wiredfool 2016-10-03 06:02:39 -07:00 committed by GitHub
commit 9440764863
3 changed files with 24 additions and 3 deletions

View File

@ -162,8 +162,11 @@ class ImageCmsProfile(object):
self._set(core.profile_open(profile), profile)
elif hasattr(profile, "read"):
self._set(core.profile_frombytes(profile.read()))
elif isinstance(profile, _imagingcms.CmsProfile):
self._set(profile)
else:
self._set(profile) # assume it's already a profile
raise TypeError("Invalid type for Profile")
def _set(self, profile, filename=None):
self.profile = profile

View File

@ -321,5 +321,17 @@ class TestImageCms(PillowTestCase):
self.assertEqual(p.viewing_condition, 'Reference Viewing Condition in IEC 61966-2-1')
self.assertEqual(p.xcolor_space, 'RGB ')
def test_profile_typesafety(self):
""" Profile init type safety
prepatch, these would segfault, postpatch they should emit a typeerror
"""
with self.assertRaises(TypeError):
ImageCms.ImageCmsProfile(0).tobytes()
with self.assertRaises(TypeError):
ImageCms.ImageCmsProfile(1).tobytes()
if __name__ == '__main__':
unittest.main()

View File

@ -1378,7 +1378,8 @@ static struct PyGetSetDef cms_profile_getsetters[] = {
static PyTypeObject CmsProfile_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"CmsProfile", sizeof(CmsProfileObject), 0,
"PIL._imagingcms.CmsProfile", /*tp_name */
sizeof(CmsProfileObject), 0,/*tp_basicsize, tp_itemsize */
/* methods */
(destructor) cms_profile_dealloc, /*tp_dealloc*/
0, /*tp_print*/
@ -1470,10 +1471,15 @@ setup_module(PyObject* m) {
d = PyModule_GetDict(m);
CmsProfile_Type.tp_new = PyType_GenericNew;
/* Ready object types */
PyType_Ready(&CmsProfile_Type);
PyType_Ready(&CmsTransform_Type);
Py_INCREF(&CmsProfile_Type);
PyModule_AddObject(m, "CmsProfile", (PyObject *)&CmsProfile_Type);
d = PyModule_GetDict(m);
v = PyUnicode_FromFormat("%d.%d", LCMS_VERSION / 100, LCMS_VERSION % 100);