镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Do not render text if image has zero width or height - #7021

Merged
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:getmask2
Mar 31, 2023
Merged

radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:getmask2

Conversation

@radarhere

Copy link
Copy Markdown
Member

In FreeTypeFont.getmask2(), only spend time rendering text if the image it will be rendered onto has a non-zero width or height.

@hugovk

hugovk commented Mar 19, 2023

Copy link
Copy Markdown
Member

Seems fine, but I wonder how often someone will draw on an image with non-zero width/height, and how much time this would actually save them?

@radarhere

radarhere commented Mar 20, 2023 •

Copy link
Copy Markdown
Member Author

getmask2() isn't actually rendering onto a user-supplied image. It is creating an image from the dimensions returned by font.getsize

Pillow/src/PIL/ImageFont.py

Lines 761 to 769 in 80edcd1

size, offset = self.font.getsize(
text, mode, direction, features, language, anchor
)
if start is None:
start = (0, 0)
size = tuple(math.ceil(size[i] + stroke_width * 2 + start[i]) for i in range(2))
offset = offset[0] - stroke_width, offset[1] - stroke_width
Image._decompression_bomb_check(size)
im = fill("RGBA" if mode == "RGBA" else "L", size, 0)

@nulano

nulano commented Mar 20, 2023

Copy link
Copy Markdown
Contributor

Sure, but this would only happen if a user tries to render a string containing spaces only, right?

In which case I'd argue that might be up to the user to prevent, as that doesn't seem like something that would be commonly done.

@radarhere radarhere closed this Mar 28, 2023
@radarhere
radarhere deleted the getmask2 branch March 28, 2023 10:46
@radarhere
radarhere restored the getmask2 branch March 31, 2023 21:49
@radarhere radarhere reopened this Mar 31, 2023
@radarhere

Copy link
Copy Markdown
Member Author

Aside from performance motivations, I was primarily thinking about https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=48708

Locally, #6842 resolved the problem with memory usage, but apparently oss-fuzz itself doesn't think it is fixed yet, so this is a second attempt. The font in the issue can trigger an OSError: raster overflow. This change avoids that.

@radarhere
radarhere merged commit eeaee75 into python-pillow:main Mar 31, 2023
@radarhere
radarhere deleted the getmask2 branch March 31, 2023 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants