Skip to content

Commit c4cc487

Browse files
authored
Merge pull request #6846 from radarhere/font_crash
Fixed null pointer dereference crash with malformed font
2 parents 7f17084 + c977526 commit c4cc487

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
STARTFONT
2+
FONT �
3+
SIZE 10
4+
FONTBOUNDINGBOX
5+
CHARS
6+
STARTCHAR
7+
ENCODING
8+
BBX 2 5
9+
ENDCHAR
10+
ENDFONT

Tests/oss-fuzz/test_fuzzers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ def test_fuzz_fonts(path):
5757
with open(path, "rb") as f:
5858
try:
5959
fuzzers.fuzz_font(f.read())
60-
except (Image.DecompressionBombError, Image.DecompressionBombWarning):
60+
except (Image.DecompressionBombError, Image.DecompressionBombWarning, OSError):
6161
pass
6262
assert True

Tests/test_font_crash.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
from PIL import Image, ImageDraw, ImageFont
4+
5+
from .helper import skip_unless_feature
6+
7+
8+
class TestFontCrash:
9+
def _fuzz_font(self, font):
10+
# from fuzzers.fuzz_font
11+
font.getbbox("ABC")
12+
font.getmask("test text")
13+
with Image.new(mode="RGBA", size=(200, 200)) as im:
14+
draw = ImageDraw.Draw(im)
15+
draw.multiline_textbbox((10, 10), "ABC\nAaaa", font, stroke_width=2)
16+
draw.text((10, 10), "Test Text", font=font, fill="#000")
17+
18+
@skip_unless_feature("freetype2")
19+
def test_segfault(self):
20+
with pytest.raises(OSError):
21+
font = ImageFont.truetype("Tests/fonts/fuzz_font-5203009437302784")
22+
self._fuzz_font(font)

src/_imagingft.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,12 @@ font_render(FontObject *self, PyObject *args) {
921921
yy = -(py + glyph_slot->bitmap_top);
922922
}
923923

924+
// Null buffer, is dereferenced in FT_Bitmap_Convert
925+
if (!bitmap.buffer && bitmap.rows) {
926+
PyErr_SetString(PyExc_OSError, "Bitmap missing for glyph");
927+
goto glyph_error;
928+
}
929+
924930
/* convert non-8bpp bitmaps */
925931
switch (bitmap.pixel_mode) {
926932
case FT_PIXEL_MODE_MONO:

0 commit comments

Comments
 (0)