Repository navigation
Closed
Description
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 ()
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]segfault with special character[/-][+]segfault drawing text with special character[/+]hugovk commentedon Nov 2, 2017
Also reproducible in Python 3.6.3 on macOS Sierra, but not in Python 2.7.14, which returns
(6, 11).wiredfool commentedon Nov 2, 2017
Yeah, I've got a fix for the version with the default font, but I'll still have to look into the ttf one.
fthiery commentedon Nov 2, 2017
Fix/test for python-pillow#2826, unchecked exception leading to null …
wiredfool commentedon Nov 2, 2017
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.
wiredfool commentedon Nov 2, 2017
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.
fthiery commentedon Nov 7, 2017
Okay thanks for fixing this, i'll triple check with the truetype font and create a new ticket with the required reproduction test.
fthiery commentedon Nov 12, 2017
Indeed, i'm failing to reproduce with a truetype font.
wiredfool commentedon Nov 12, 2017
Thanks for checking. Let us know if it does ever happen.