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

Conversation

radarhere
Copy link
Member

Resolves #3853, alternative to #3854

The issue reports that loading a non-font as bytes into ImageFont.truetype causes a crash. The alternative PR concludes that it is because of memory being free twice - one in getfont, and once in font_dealloc.

Pillow/src/_imagingft.c

Lines 316 to 318 in 9d5a50a

if (self->font_bytes) {
PyMem_Free(self->font_bytes);
}

Pillow/src/_imagingft.c

Lines 1034 to 1041 in 9d5a50a

font_dealloc(FontObject* self)
{
if (self->face) {
FT_Done_Face(self->face);
}
if (self->font_bytes) {
PyMem_Free(self->font_bytes);
}

The alternative PR solves this by removing the PyMem_Free from getfont. This PR suggests instead setting self->font_bytes to null after the memory is freed the first time.

Also, the alternative PR points out that once the error is raised, os.path.basename(font) is called - which doesn't work if font is a file-like object. So this PR also fixes that.

@hugovk
Copy link
Member

hugovk commented Jun 27, 2019

Thanks, let's go with this one as it's a smaller change.

@hugovk hugovk merged commit b584635 into python-pillow:master Jun 27, 2019
@radarhere radarhere deleted the non_font_bytes branch June 27, 2019 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash on trying to load corrupted font as file handle
2 participants