Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crash when loading non-font bytes #3912

Merged
merged 1 commit into from Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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