Skip to content

Commit

Permalink
Fixed crash when loading non-font bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 23, 2019
1 parent 32d1050 commit 7bb16de
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Tests/test_imagefont.py
Expand Up @@ -420,6 +420,10 @@ def test_load_path_not_found(self):
self.assertRaises(IOError, ImageFont.load_path, filename)
self.assertRaises(IOError, ImageFont.truetype, filename)

def test_load_non_font_bytes(self):
with open("Tests/images/hopper.jpg", "rb") as f:
self.assertRaises(IOError, ImageFont.truetype, f)

def test_default_font(self):
# Arrange
txt = 'This is a "better than nothing" default font.'
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/ImageFont.py
Expand Up @@ -545,6 +545,8 @@ def freetype(font):
try:
return freetype(font)
except IOError:
if not isPath(font):
raise
ttf_filename = os.path.basename(font)

dirs = []
Expand Down
1 change: 1 addition & 0 deletions src/_imagingft.c
Expand Up @@ -315,6 +315,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
if (error) {
if (self->font_bytes) {
PyMem_Free(self->font_bytes);
self->font_bytes = NULL;
}
Py_DECREF(self);
return geterror(error);
Expand Down

0 comments on commit 7bb16de

Please sign in to comment.