Coverage for /pythoncovmergedfiles/medio/medio/src/Pillow/Tests/oss-fuzz/fuzzers.py: 90%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3import io
4import warnings
6from PIL import Image, ImageDraw, ImageFile, ImageFilter, ImageFont
9def enable_decompressionbomb_error() -> None:
10 ImageFile.LOAD_TRUNCATED_IMAGES = True
11 warnings.filterwarnings("ignore")
12 warnings.simplefilter("error", Image.DecompressionBombWarning)
15def disable_decompressionbomb_error() -> None:
16 ImageFile.LOAD_TRUNCATED_IMAGES = False
17 warnings.resetwarnings()
20def fuzz_image(data: bytes) -> None:
21 # This will fail on some images in the corpus, as we have many
22 # invalid images in the test suite.
23 with Image.open(io.BytesIO(data)) as im:
24 im.rotate(45)
25 im.filter(ImageFilter.DETAIL)
26 im.save(io.BytesIO(), "BMP")
29def fuzz_font(data: bytes) -> None:
30 wrapper = io.BytesIO(data)
31 try:
32 font = ImageFont.truetype(wrapper)
33 except OSError:
34 # Catch pcf/pilfonts/random garbage here. They return
35 # different font objects.
36 return
38 font.getbbox("ABC")
39 font.getmask("test text")
40 with Image.new(mode="RGBA", size=(200, 200)) as im:
41 draw = ImageDraw.Draw(im)
42 draw.multiline_textbbox((10, 10), "ABC\nAaaa", font, stroke_width=2)
43 draw.text((10, 10), "Test Text", font=font, fill="#000")