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

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

Merged
merged 1 commit into from Mar 31, 2023

Conversation

radarhere
Copy link
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
Copy link
Member

hugovk commented Mar 19, 2023

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
Copy link
Member Author

radarhere commented Mar 20, 2023

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
Copy link
Contributor

nulano commented Mar 20, 2023

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
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
121 checks passed
@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.

None yet

3 participants