Skip to content

If bitmap buffer is empty, do not render anything#8324

Merged
hugovk merged 2 commits intopython-pillow:mainfrom
radarhere:bitmap_buffer
Mar 20, 2026
Merged

If bitmap buffer is empty, do not render anything#8324
hugovk merged 2 commits intopython-pillow:mainfrom
radarhere:bitmap_buffer

Conversation

@radarhere
Copy link
Copy Markdown
Member

@radarhere radarhere commented Aug 23, 2024

Resolves #8272

#6846 added an error if the bitmap buffer is empty.

Pillow/src/_imagingft.c

Lines 1028 to 1032 in d6cfebd

// Null buffer, is dereferenced in FT_Bitmap_Convert
if (!bitmap.buffer && bitmap.rows) {
PyErr_SetString(PyExc_OSError, "Bitmap missing for glyph");
goto glyph_error;
}

However, this new issue found the buffer is empty for a space character in a particular font, which seems like a realistic scenario, and has been confirmed as valid by one of the FreeType maintainers - #8272 (comment)

So if the bitmap buffer is empty, this PR just doesn't draw anything, uses the advances to update the position for the next character, and moves on.

draw.text((10, 10), "Test Text", font=font, fill="#000")

@skip_unless_feature("freetype2")
@skip_unless_feature_version("freetype2", "2.12.0")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the version here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test intended to catch OSError: Bitmap missing for glyph from

Pillow/src/_imagingft.c

Lines 1028 to 1032 in b6f90c4

// Null buffer, is dereferenced in FT_Bitmap_Convert
if (!bitmap.buffer && bitmap.rows) {
PyErr_SetString(PyExc_OSError, "Bitmap missing for glyph");
goto glyph_error;
}

For FreeType2 versions before 2.12.0, FT_New_Face is returning an Unknown_File_Format error. It does that even on main at the moment, it's just not obvious because both are OSError. If I increase the specificity of the test, you can see this.

FreeType2 2.12.0 was released in March 2022, before #6846, so I expect this has been the case the whole time. Now that I've fixed the OSError: Bitmap missing for glyph error, the other error becomes obvious.

This was a font generated by OSS-Fuzz, so its purpose is not to be valid, but merely to trigger a security problem. We could modify the file to be valid, but in principle it would seem to be better to not modify test scenarios over time.

I suppose I could modify the test to run on FreeType2 < 2.12.0 as well, and just catch the error for that scenario if you want.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation.

@nulano
Copy link
Copy Markdown
Contributor

nulano commented Aug 23, 2024

We might want to add a test that triggers the if (stroker != NULL) branch in font_render to make sure that passing such a glyph to the stroker API is safe.

@radarhere
Copy link
Copy Markdown
Member Author

You're concerned about passing a glyph where bitmap buffer is empty to this block? test_bitmap_font_stroke actually already does this.

@apodtele
Copy link
Copy Markdown

Note then the glyph outline in question is not empty but degenerate. It contains two points but still produces a blank space. In this particular case, it results in a 0×1 bitmap, aka one empty row. Do not check for bitmap.rows, or check both dimentions

@radarhere
Copy link
Copy Markdown
Member Author

The glyph outline may not be empty, but the bitmap buffer is. From my understanding, we're safe to skip drawing and just advance if that is the case. I presume the size of the glyph does not affect the advance.

@apodtele
Copy link
Copy Markdown

Correct, just advance if the buffer is NULL regardless of its dimensions. You have sufficient error checking from FreeType calls.

@radarhere radarhere requested a review from hugovk January 3, 2026 04:56
@hugovk hugovk merged commit 6ab139e into python-pillow:main Mar 20, 2026
1 check passed
@radarhere radarhere deleted the bitmap_buffer branch March 20, 2026 14:00
luketainton pushed a commit to luketainton/repos_webexmemebot that referenced this pull request Apr 1, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [pillow](https://github.com/python-pillow/Pillow) ([changelog](https://github.com/python-pillow/Pillow/releases)) | `<12.1.2,>=12.1.1` → `<12.2.1,>=12.2.0` | ![age](https://developer.mend.io/api/mc/badges/age/pypi/pillow/12.2.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/pillow/12.1.1/12.2.0?slim=true) |

---

### Release Notes

<details>
<summary>python-pillow/Pillow (pillow)</summary>

### [`v12.2.0`](https://github.com/python-pillow/Pillow/releases/tag/12.2.0)

[Compare Source](python-pillow/Pillow@12.1.1...12.2.0)

<https://pillow.readthedocs.io/en/stable/releasenotes/12.2.0.html>

#### Documentation

- Update 12.2.0 release notes [#&#8203;9522](python-pillow/Pillow#9522) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Add loader plugins: AMOS abk, Atari Degas, 40+ more obscure formats via Netpbm [#&#8203;9482](python-pillow/Pillow#9482) \[[@&#8203;bitplane](https://github.com/bitplane)]
- Update Python versions [#&#8203;9515](python-pillow/Pillow#9515) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Jeffrey A. Clark -> Jeffrey 'Alex' Clark [#&#8203;9513](python-pillow/Pillow#9513) \[[@&#8203;aclark4life](https://github.com/aclark4life)]
- Add release notes for [#&#8203;9394](python-pillow/Pillow#9394), [#&#8203;9419](python-pillow/Pillow#9419) and [#&#8203;9456](python-pillow/Pillow#9456) [#&#8203;9467](python-pillow/Pillow#9467) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Add Amiga Workbench .info loader to 3rd party plugins list [#&#8203;9459](python-pillow/Pillow#9459) \[[@&#8203;bitplane](https://github.com/bitplane)]
- Merge PFM documentation into PPM [#&#8203;9434](python-pillow/Pillow#9434) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update macOS tested Pillow versions [#&#8203;9431](python-pillow/Pillow#9431) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Fix CVE number [#&#8203;9430](python-pillow/Pillow#9430) \[[@&#8203;hugovk](https://github.com/hugovk)]

#### Dependencies

- Update xz to 5.8.3 [#&#8203;9523](python-pillow/Pillow#9523) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update libjpeg-turbo to 3.1.4.1 [#&#8203;9507](python-pillow/Pillow#9507) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update libpng to 1.6.56 [#&#8203;9499](python-pillow/Pillow#9499) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update freetype to 2.14.3 [#&#8203;9485](python-pillow/Pillow#9485) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Updated libavif to 1.4.1 [#&#8203;9479](python-pillow/Pillow#9479) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Updated harfbuzz to 13.2.1 [#&#8203;9461](python-pillow/Pillow#9461) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update Ghostscript to 10.7.0 [#&#8203;9469](python-pillow/Pillow#9469) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update harfbuzz to 13.0.1 [#&#8203;9453](python-pillow/Pillow#9453) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update libavif to 1.4.0 [#&#8203;9460](python-pillow/Pillow#9460) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update freetype to 2.14.2 [#&#8203;9449](python-pillow/Pillow#9449) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update actions/download-artifact action to v8 [#&#8203;9451](python-pillow/Pillow#9451) \[@&#8203;[renovate\[bot\]](https://github.com/apps/renovate)]
- Updated libpng to 1.6.55 [#&#8203;9425](python-pillow/Pillow#9425) \[[@&#8203;radarhere](https://github.com/radarhere)]

#### Testing

- Cleanup .spider extension in the same test where it is added [#&#8203;9517](python-pillow/Pillow#9517) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Run tests in parallel via tox for 3.5x speedup [#&#8203;9516](python-pillow/Pillow#9516) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Enable colour in CI logs [#&#8203;9486](python-pillow/Pillow#9486) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Update Ghostscript to 10.7.0 [#&#8203;9469](python-pillow/Pillow#9469) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Simplify TGA test code [#&#8203;9477](python-pillow/Pillow#9477) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Update tests to check for ValueError when encoding an empty image [#&#8203;9464](python-pillow/Pillow#9464) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Upgrade CI from `macos-15-intel` to `macos-26-intel` [#&#8203;9454](python-pillow/Pillow#9454) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Add check-case-conflict hook [#&#8203;9446](python-pillow/Pillow#9446) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Specify platform when pulling docker image [#&#8203;9440](python-pillow/Pillow#9440) \[[@&#8203;radarhere](https://github.com/radarhere)]
- GHA: Cache libavif and webp builds for Ubuntu [#&#8203;9437](python-pillow/Pillow#9437) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Update macOS tested Pillow versions [#&#8203;9431](python-pillow/Pillow#9431) \[[@&#8203;radarhere](https://github.com/radarhere)]

#### Other changes

- Check calloc return value [#&#8203;9527](python-pillow/Pillow#9527) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Check all allocs in the Arrow tree [#&#8203;9488](python-pillow/Pillow#9488) \[[@&#8203;wiredfool](https://github.com/wiredfool)]
- Reject non-numeric elements inside list coords [#&#8203;9526](python-pillow/Pillow#9526) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Move variable declaration inside define [#&#8203;9525](python-pillow/Pillow#9525) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Resize tall images vertically first [#&#8203;9524](python-pillow/Pillow#9524) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Avoid overflow by not adding extents together [#&#8203;9520](python-pillow/Pillow#9520) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Use long for glyph position [#&#8203;9518](python-pillow/Pillow#9518) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Raise an error if the trailer chain loops back on itself [#&#8203;9519](python-pillow/Pillow#9519) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Only read as much data from gzip-decompressed data as necessary [#&#8203;9521](python-pillow/Pillow#9521) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Allow None extents in C setimage() [#&#8203;9504](python-pillow/Pillow#9504) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Use critical sections to protect FontObject [#&#8203;9498](python-pillow/Pillow#9498) \[[@&#8203;colesbury](https://github.com/colesbury)]
- Add ImageText.Text.wrap() to wrap text [#&#8203;9286](python-pillow/Pillow#9286) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Always call StubHandler open() when opening StubImageFile [#&#8203;9412](python-pillow/Pillow#9412) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Improved BCn overflow check [#&#8203;9043](python-pillow/Pillow#9043) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Image will never be None [#&#8203;9512](python-pillow/Pillow#9512) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Raise EOFError when seeking too far in PSD [#&#8203;9388](python-pillow/Pillow#9388) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Raise error if ImageGrab subprocess gives non-zero returncode [#&#8203;9321](python-pillow/Pillow#9321) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Allow for different palette entry sizes when correcting BMP pixel data offset [#&#8203;9472](python-pillow/Pillow#9472) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Ignore unspecified extra samples for TIFF separate planar configuration [#&#8203;9514](python-pillow/Pillow#9514) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Add PERF to lint and fix findings [#&#8203;9510](python-pillow/Pillow#9510) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Switch iOS back to macos-15-intel [#&#8203;9509](python-pillow/Pillow#9509) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Catch struct.error [#&#8203;9505](python-pillow/Pillow#9505) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Check PyCapsule\_GetPointer and PyBytes\_FromStringAndSize return values [#&#8203;9508](python-pillow/Pillow#9508) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Fix missing null dereference checks [#&#8203;9489](python-pillow/Pillow#9489) \[[@&#8203;wiredfool](https://github.com/wiredfool)]
- CI: Retry failed downloads [#&#8203;9506](python-pillow/Pillow#9506) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Use PyModule\_AddObjectRef [#&#8203;9503](python-pillow/Pillow#9503) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Release reference to encoder on error [#&#8203;9500](python-pillow/Pillow#9500) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Fixed AVIF and WEBP dealloc [#&#8203;9501](python-pillow/Pillow#9501) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Check PyType\_Ready return values [#&#8203;9502](python-pillow/Pillow#9502) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Check if PyObject\_CallMethod result is NULL [#&#8203;9494](python-pillow/Pillow#9494) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Do not use palette from grayscale or bilevel colorspace when reading JPEG2000 images [#&#8203;9468](python-pillow/Pillow#9468) \[[@&#8203;radarhere](https://github.com/radarhere)]
- If TGA v2 extension area specifies no alpha, fill alpha channel [#&#8203;9478](python-pillow/Pillow#9478) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Set image pixels individually on 32-bit Windows [#&#8203;9492](python-pillow/Pillow#9492) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Add error messages before returning NULL when encoding [#&#8203;9493](python-pillow/Pillow#9493) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Fix `_getxy` refcount leaks [#&#8203;9487](python-pillow/Pillow#9487) \[[@&#8203;hugovk](https://github.com/hugovk)]
- Fix invalid test font [#&#8203;9483](python-pillow/Pillow#9483) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Add Exif tag "FrameRate" [#&#8203;9470](python-pillow/Pillow#9470) \[[@&#8203;zhiyuanouyang](https://github.com/zhiyuanouyang)]
- Support reading JPEG2000 images with CMYK palettes [#&#8203;9456](python-pillow/Pillow#9456) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Simplify `setimage()` by always passing extents [#&#8203;9395](python-pillow/Pillow#9395) \[[@&#8203;radarhere](https://github.com/radarhere)]
- If bitmap buffer is empty, do not render anything [#&#8203;8324](python-pillow/Pillow#8324) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Change to ValueError when encoding an empty image [#&#8203;9394](python-pillow/Pillow#9394) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Add FontFile.to\_imagefont() [#&#8203;9419](python-pillow/Pillow#9419) \[[@&#8203;fjhenigman](https://github.com/fjhenigman)]
- \[pre-commit.ci] pre-commit autoupdate [#&#8203;9450](python-pillow/Pillow#9450) \[@&#8203;[pre-commit-ci\[bot\]](https://github.com/apps/pre-commit-ci)]
- Use walrus operator [#&#8203;9448](python-pillow/Pillow#9448) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Only close file handle in ImagePalette.save() if it was opened internally [#&#8203;9444](python-pillow/Pillow#9444) \[[@&#8203;bysiber](https://github.com/bysiber)]
- Fix `self.decode` typo [#&#8203;9445](python-pillow/Pillow#9445) \[[@&#8203;bysiber](https://github.com/bysiber)]
- Fix BMP RLE delta escape reading from wrong file position [#&#8203;9443](python-pillow/Pillow#9443) \[[@&#8203;bysiber](https://github.com/bysiber)]
- Correct error check when encoding AVIF images [#&#8203;9442](python-pillow/Pillow#9442) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Fix unexpected error when saving zero dimension images [#&#8203;9391](python-pillow/Pillow#9391) \[[@&#8203;radarhere](https://github.com/radarhere)]
- Use uppercase format ID for PALM [#&#8203;9435](python-pillow/Pillow#9435) \[[@&#8203;radarhere](https://github.com/radarhere)]

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xMDIuNiIsInVwZGF0ZWRJblZlciI6IjQzLjEwMi42IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJ0eXBlL2RlcGVuZGVuY2llcyJdfQ==-->

Reviewed-on: https://git.tainton.uk/repos/webexmemebot/pulls/579
Reviewed-by: Luke Tainton <luke@tainton.uk>
Co-authored-by: renovate[bot] <renovate-bot@git.tainton.uk>
Co-committed-by: renovate[bot] <renovate-bot@git.tainton.uk>
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.

Bitmap missing for glyph

4 participants