python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.09k stars 2.21k forks source link

Crash when using `Draw.text` with invalid type #8307

Closed jonashaag closed 1 week ago

jonashaag commented 4 weeks ago

What did you do?

from PIL import Image, ImageDraw
img = Image.new("L", (1, 1))
ImageDraw.Draw(img).text((0, 0), ("a", "b"))

What did you expect to happen?

Should raise a TypeError

What actually happened?

It crashed.

What are your OS, Python and Pillow versions?

--------------------------------------------------------------------
Pillow 10.4.0
Python 3.12.5 | packaged by conda-forge | (main, Aug  8 2024, 18:32:50) [Clang 16.0.6 ]
--------------------------------------------------------------------
Python executable is .../.pixi/envs/default/bin/python3
System Python files loaded from .../.pixi/envs/default
--------------------------------------------------------------------
Python Pillow modules loaded from .../.pixi/envs/default/lib/python3.12/site-packages/PIL
Binary Pillow modules loaded from .../.pixi/envs/default/lib/python3.12/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.4.0
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 3.0.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1
--- LIBTIFF support ok, loaded 4.6.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
radarhere commented 4 weeks ago

I've created #8308 to resolve this.

hugovk commented 4 weeks ago

Interesting that I can reproduce it with Python 3.9, 3.11 and 3.12:

Python 3.9.13 (v3.9.13:6de2ca5339, May 17 2022, 11:37:23)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image, ImageDraw
>>> img = Image.new("L", (1, 1))
>>> ImageDraw.Draw(img).text((0, 0), ("a", "b"))
[1]    25375 segmentation fault  python3.9

But with the Python 3.13 release candidate, I get TypeError: expected bytes, tuple found:

Python 3.13.0rc1 (v3.13.0rc1:e4a3e786a5e, Jul 31 2024, 19:49:53) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image, ImageDraw
... img = Image.new("L", (1, 1))
... ImageDraw.Draw(img).text((0, 0), ("a", "b"))
...
Traceback (most recent call last):
  File "<python-input-0>", line 3, in <module>
    ImageDraw.Draw(img).text((0, 0), ("a", "b"))
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/ImageDraw.py", line 696, in text
    draw_text(ink)
    ~~~~~~~~~^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/ImageDraw.py", line 635, in draw_text
    mask, offset = font.getmask2(  # type: ignore[union-attr,misc]
                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        text,
        ^^^^^
    ...<9 lines>...
        **kwargs,
        ^^^^^^^^^
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/ImageFont.py", line 607, in getmask2
    return self.font.render(
           ~~~~~~~~~~~~~~~~^
        text,
        ^^^^^
    ...<9 lines>...
        start[1],
        ^^^^^^^^^
    )
    ^
TypeError: expected bytes, tuple found
>>>