Skip to content

segfault drawing text with special character #2826

Closed

Description

@fthiery

What did you do?

Tried to draw some text containing special characters (from a tsv file). Getting a segfault. Note that the issue happens with a "special" font (ex: arial.ttf), but it also reproduces with the default font.

What versions of Pillow and Python are you using?

python: 3.6.3
pillow: 4.3.0
system: Arch Linux
LANG=fr_FR.UTF-8

Code

from PIL import ImageFont
font = ImageFont.load_default()
font.getsize("’")

Gdb shows:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff74a0d6a in PyBytes_AsStringAndSize () from /usr/lib/libpython3.6m.so.1.0
(gdb) bt
#0  0x00007ffff74a0d6a in PyBytes_AsStringAndSize () from /usr/lib/libpython3.6m.so.1.0
#1  0x00007ffff61b3314 in _font_text_asBytes ()
   from /usr/lib/python3.6/site-packages/PIL/_imaging.cpython-36m-x86_64-linux-gnu.so
#2  0x00007ffff61b3361 in ?? ()
   from /usr/lib/python3.6/site-packages/PIL/_imaging.cpython-36m-x86_64-linux-gnu.so
#3  0x00007ffff7419af0 in _PyCFunction_FastCallDict () from /usr/lib/libpython3.6m.so.1.0
#4  0x00007ffff73c908b in ?? () from /usr/lib/libpython3.6m.so.1.0
#5  0x00007ffff73aeb9a in _PyEval_EvalFrameDefault () from /usr/lib/libpython3.6m.so.1.0
#6  0x00007ffff73ac2b4 in ?? () from /usr/lib/libpython3.6m.so.1.0
#7  0x00007ffff73c8c4f in ?? () from /usr/lib/libpython3.6m.so.1.0
#8  0x00007ffff73c914e in ?? () from /usr/lib/libpython3.6m.so.1.0
#9  0x00007ffff73aeb9a in _PyEval_EvalFrameDefault () from /usr/lib/libpython3.6m.so.1.0
#10 0x00007ffff73ad1d8 in PyEval_EvalCodeEx () from /usr/lib/libpython3.6m.so.1.0
#11 0x00007ffff73ae06c in PyEval_EvalCode () from /usr/lib/libpython3.6m.so.1.0
#12 0x00007ffff748f2d4 in ?? () from /usr/lib/libpython3.6m.so.1.0
#13 0x00007ffff7491cc1 in PyRun_FileExFlags () from /usr/lib/libpython3.6m.so.1.0
#14 0x00007ffff7491ec4 in PyRun_SimpleFileExFlags () from /usr/lib/libpython3.6m.so.1.0
#15 0x00007ffff748e160 in Py_Main () from /usr/lib/libpython3.6m.so.1.0
#16 0x0000555555554c39 in main ()

Activity

  1. changed the title [-]segfault with special character[/-] [+]segfault drawing text with special character[/+] on Nov 2, 2017
  2. hugovk commented on Nov 2, 2017

    @hugovk
    Member

    Also reproducible in Python 3.6.3 on macOS Sierra, but not in Python 2.7.14, which returns (6, 11).

  3. wiredfool commented on Nov 2, 2017

    @wiredfool
    Member

    Yeah, I've got a fix for the version with the default font, but I'll still have to look into the ttf one.

  4. fthiery commented on Nov 2, 2017

    @fthiery
    Author
  5. added a commit that references this issue on Nov 2, 2017
  6. wiredfool commented on Nov 2, 2017

    @wiredfool
    Member

    So, what's happening here (at least on the default font) is that it only covers the latin-1 character set, or 0-255. There's a conversion from unicode to latin1 in there which fails, is not checked, and then the null pointer is used and there's a segfault. This will happen for any unicode object with a code point > 255 passed into this routine.

    I was looking at this routine earlier and found an off by one bug leading to an out of bounds read. Both of these bugs were added when fixing a memory leak. And there's unreachable code as well. So I guess I win the dangerous C bugs trophy for tonight.

  7. wiredfool commented on Nov 2, 2017

    @wiredfool
    Member

    I can't reproduce this using a truetype font. If it does happen there, it's got to be a completely different code path and a different bug than the one for the default font, as the routine that has the bug there is only used for the pilfont path.

  8. fthiery commented on Nov 7, 2017

    @fthiery
    Author

    Okay thanks for fixing this, i'll triple check with the truetype font and create a new ticket with the required reproduction test.

  9. fthiery commented on Nov 12, 2017

    @fthiery
    Author

    Indeed, i'm failing to reproduce with a truetype font.

  10. wiredfool commented on Nov 12, 2017

    @wiredfool
    Member

    Thanks for checking. Let us know if it does ever happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions