Simplify FreeTypeFont.getmask2() by nulano · Pull Request #7645 · python-pillow/Pillow

Changes proposed in this pull request:

  • Call Image._decompression_bomb_check directly in fill rather than duplicating its code. If an error is raised, it is propagated via the if (image == NULL) branch.
  • On error, delete the Python image object by releasing the only reference to it (causing Python to call its tp_dealloc slot, i.e. _dealloc in _imaging.c) instead of just freeing the memory it points to (added in Improved checks in font_render #7218).
  • Avoid nonlocal variables (to simplify adding type hints in a future PR by avoiding the need for a typing.cast or assert size is not None).

This partially reverts changes from #7246, which was connected to an oss-fuzz error, but I do not see any added tests or reproduction steps. IIUC, the original error was a memory leak, which I avoid in a different way in this PR:

The original code (before #7246) returned image via O format (returning a new reference) and did not Py_DECREF (causing a memory leak).
#7246 instead returns a reference via a nonlocal Python variable and added Py_DECREF.
In this PR I have removed the nonlocal variable and return the existing reference via N format.
See https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue for the difference between O and N.