Skip to content

Commit 2d32ccf

Browse files
committed
FIX: reject out-of-range charmap index in FT2Font.set_charmap
FT2Font.set_charmap(i) only checked the upper bound (i >= num_charmaps). A negative i passed the check and was used to index face->charmaps[i], an out-of-bounds read whose result was then dereferenced by FT_Set_Charmap, crashing the interpreter (e.g. set_charmap(-1)). Reject negative indices as well, raising the same RuntimeError already used for too-large indices. Valid indices in [0, num_charmaps) are unaffected.
1 parent 1e6bcaf commit 2d32ccf

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/matplotlib/tests/test_ft2font.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ def enc(name):
346346
# Though the encoding is different, the glyph should be the same.
347347
assert unic[u] == armn[m]
348348

349+
# Out-of-range charmap indices must be rejected rather than indexing out of
350+
# bounds. A negative index previously passed the upper-bound-only check and
351+
# read face->charmaps[i] out of bounds.
352+
with pytest.raises(RuntimeError, match='exceeds the available number'):
353+
font.set_charmap(-1)
354+
with pytest.raises(RuntimeError, match='exceeds the available number'):
355+
font.set_charmap(font.num_charmaps)
356+
349357

350358
_expected_sfnt_names = {
351359
'DejaVu Sans': {

src/ft2font.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void FT2Font::_set_transform(
269269

270270
void FT2Font::set_charmap(int i)
271271
{
272-
if (i >= face->num_charmaps) {
272+
if (i < 0 || i >= face->num_charmaps) {
273273
throw std::runtime_error("i exceeds the available number of char maps");
274274
}
275275
FT_CHECK(FT_Set_Charmap, face, face->charmaps[i]);

0 commit comments

Comments
 (0)