Fixed crash when loading non-font bytes #3912
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ingetfont
, and once infont_dealloc
.Pillow/src/_imagingft.c
Lines 316 to 318 in 9d5a50a
Pillow/src/_imagingft.c
Lines 1034 to 1041 in 9d5a50a
The alternative PR solves this by removing the
PyMem_Free
fromgetfont
. This PR suggests instead settingself->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 iffont
is a file-like object. So this PR also fixes that.